分享

TreeListView

 牛人的尾巴 2016-04-05

TreeListView

, 6 Apr 2008 CPOL
   4.75 (15 votes)
TreeView with multiple columns
treelistview.jpg

Introduction

This control is supposed to display hierarchical data in a TreeView while providing the possibility to append additional information to each item.

Background

I needed to present data in a hierarchical structure but it was required to display additional information for each item. At first, I appended all this information to the TreeNodeItem's text which was quite unclear for the user.

Using the Code

To use the multi column TreeView in your own application, declare the control in any of your windows or controls. TreeListView exposes all properties of ListView plus AllowsColumnReorder and Columns.

Hide   Copy Code
<local:TreeListView AllowsColumnReorder="True">
    <local:TreeListView.Columns>
        ...
    </local:TreeListView.Columns>
    ...
</local:TreeListView>

Both properties correlate with the AllowsColumnReorder and Columns properties of GridView which is documented here.

Hierarchical Data Template

In order to provide the tree with a way to retrieve child nodes, you must either declare a HierarchicalDataTemplate or a DataTemplateSelector.

TreeListView is only going to use the ItemsSource property of the HierarchicalDataTemplate or DataTemplateSelector because all information is visualized in the same manner (in rows). Therefore, a declaration as follows is sufficient:

Hide   Copy Code
<local:TreeListView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}"/>
</local:TreeListView.ItemTemplate>

Columns

It is vital to declare at least one column which the TreeListView is going to use to visualize the data. MSDN provides sufficient information about declaring GridViewColumns so I am not going to go into any details.

The only new thing about declaring GridViewColumns is that at least one column must contain a TreeListViewExpander which is a ToggleButton that can expand its row and indents all contents in its column.

Hide   Copy Code
<GridViewColumn>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!--<span class="code-comment">The Expander Button (can be used in any column 
                (typically the first one))--></span>
                <local:TreeListViewExpander/>
                <!--<span class="code-comment">The data to display in this column--></span>
                <TextBlock Text="{Binding}"/>
            </StackPanel>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

And that's it!

Points of Interest

It would probably be a good idea to implement all features of GridView. Unfortunately I do not have the time to do that right now.

History

  • 6th April, 2008
    • First release
    • Fixed file download

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多