Click here to Skip to main content
11,428,497 members (63,396 online)
Click here to Skip to main content

TreeListView

, 6 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

<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:

<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.

<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

tillmyspace

United States United States
No Biography provided

Comments and Discussions

 
QuestionDatabinding to 'IsSelected' and 'IsExpanded' properties of view model Pin
HollyVi8-Sep-13 7:08
memberHollyVi8-Sep-13 7:08 
QuestionKeyboard navigation and .NET 4.5 Pin
jenschrrh17-May-13 12:28
memberjenschrrh17-May-13 12:28 
AnswerRe: Keyboard navigation and .NET 4.5 Pin
jenschrrh17-May-13 13:40
memberjenschrrh17-May-13 13:40 
GeneralMy vote of 5 Pin
STRICQ12-May-13 10:41
memberSTRICQ12-May-13 10:41 
QuestionFrozenColumnCount Pin
edmolko27-Nov-12 6:38
memberedmolko27-Nov-12 6:38 
QuestionHierarchicalDataTemplate Pin
Member 144952325-Jul-12 5:22
memberMember 144952325-Jul-12 5:22 
Questionhow binding with use entity framework Pin
urmcoder13-Dec-11 14:17
memberurmcoder13-Dec-11 14:17 
QuestionDifferent display for nodes in main (=first) column Pin
Member 81586592-Sep-11 8:20
memberMember 81586592-Sep-11 8:20 
QuestionFix Columns? Pin
klgott21-Mar-11 5:20
memberklgott21-Mar-11 5:20 
AnswerRe: Fix Columns? Pin
Zoot18-Jul-11 9:39
memberZoot18-Jul-11 9:39 
GeneralRe: Fix Columns? Pin
edmolko27-Nov-12 2:45
memberedmolko27-Nov-12 2:45 
GeneralItemsSource binding to List<> [modified] Pin
msabatini9-Sep-10 23:08
membermsabatini9-Sep-10 23:08 
GeneralRe: ItemsSource binding to List Pin
msabatini12-Sep-10 23:42
membermsabatini12-Sep-10 23:42 
GeneralRe: ItemsSource binding to List Pin
msabatini13-Sep-10 0:45
membermsabatini13-Sep-10 0:45 
GeneralMy vote of 5 Pin
ilke can22-Jul-10 22:46
memberilke can22-Jul-10 22:46 
Questionlazy loading? Pin
omid rezaei hanjani22-May-10 2:38
memberomid rezaei hanjani22-May-10 2:38 
QuestionSorting Pin
sannaroby19-May-10 23:23
membersannaroby19-May-10 23:23 
AnswerRe: Sorting Pin
Moshe B13-Jan-12 0:57
memberMoshe B13-Jan-12 0:57 
QuestionDynamic Binding Pin
Carlos Gutierrez20-Mar-09 10:41
memberCarlos Gutierrez20-Mar-09 10:41 
GeneralFile Pin
gurveysc8-Apr-08 4:55
membergurveysc8-Apr-08 4:55 
GeneralRe: File Pin
tillmyspace8-Apr-08 21:32
membertillmyspace8-Apr-08 21:32 
GeneralRe: File Pin
Member 38002106-Aug-09 5:06
memberMember 38002106-Aug-09 5:06 
GeneralRe: File Pin
tillmyspace9-Apr-08 11:06
membertillmyspace9-Apr-08 11:06 
GeneralRe: File Pin
gurveysc9-Apr-08 11:17
membergurveysc9-Apr-08 11:17 
GeneralRe: File Pin
Thoris25-Jul-08 6:20
memberThoris25-Jul-08 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 6 Apr 2008
Article Copyright 2008 by tillmyspace
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid