Click here to Skip to main content
Click here to Skip to main content

TreeListView

By , 6 Apr 2008
 
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">
                <!--The Expander Button (can be used in any column 
                (typically the first one))-->
                <local:TreeListViewExpander/>
                <!--The data to display in this column-->
                <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)

About the Author

tillmyspace
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionKeyboard navigation and .NET 4.5memberjenschrrh17 May '13 - 11:28 
AnswerRe: Keyboard navigation and .NET 4.5memberjenschrrh17 May '13 - 12:40 
GeneralMy vote of 5memberSTRICQ12 May '13 - 9:41 
QuestionFrozenColumnCountmemberedmolko27 Nov '12 - 5:38 
QuestionHierarchicalDataTemplatememberMember 144952325 Jul '12 - 4:22 
Questionhow binding with use entity frameworkmemberurmcoder13 Dec '11 - 13:17 
QuestionDifferent display for nodes in main (=first) columnmemberMember 81586592 Sep '11 - 7:20 
QuestionFix Columns?memberklgott21 Mar '11 - 4:20 
AnswerRe: Fix Columns?memberZoot18 Jul '11 - 8:39 
GeneralRe: Fix Columns?memberedmolko27 Nov '12 - 1:45 
GeneralItemsSource binding to List<> [modified]membermsabatini9 Sep '10 - 22:08 
GeneralRe: ItemsSource binding to Listmembermsabatini12 Sep '10 - 22:42 
GeneralRe: ItemsSource binding to Listmembermsabatini12 Sep '10 - 23:45 
GeneralMy vote of 5memberilke can22 Jul '10 - 21:46 
Questionlazy loading?memberomid rezaei hanjani22 May '10 - 1:38 
QuestionSortingmembersannaroby19 May '10 - 22:23 
AnswerRe: SortingmemberMoshe B12 Jan '12 - 23:57 
QuestionDynamic BindingmemberCarlos Gutierrez20 Mar '09 - 9:41 
GeneralFilemembergurveysc8 Apr '08 - 3:55 
GeneralRe: Filemembertillmyspace8 Apr '08 - 20:32 
GeneralRe: FilememberMember 38002106 Aug '09 - 4:06 
GeneralRe: Filemembertillmyspace9 Apr '08 - 10:06 
GeneralRe: Filemembergurveysc9 Apr '08 - 10:17 
GeneralRe: FilememberThoris25 Jul '08 - 5:20 
GeneralRe: Filemembergurveysc25 Jul '08 - 5:33 
GeneralRe: FilememberSunil Jampa28 Sep '08 - 20:50 

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

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