Click here to Skip to main content
14,389,512 members

WPF TreeListView Control

Rate this:
4.89 (66 votes)
Please Sign up or sign in to vote.
4.89 (66 votes)
23 Aug 2012Apache
This article describes the usage of custom WPF TreeListView control comparing with basic TreeView
Image 1


This articles explores the problems of standard WPF TreeView controls and describes a better way to display hierarchical data using a custom TreeListView control.

Background of TreeView

Windows Forms TreeView has quite a limited functionality and provides no easy way to extend it. WPF TreeView seems like a major step forward, at first glance. But in real application the lack of features like multiselection or multicolumn view become apparent. Moreover, things that were quite easy to do in Windows Forms are now much more complex in WPF because there's no simple way to get the container item for your business object displayed in the TreeView. For example, if you need to expand the currently selected node you have to use ItemContainerGenerator as described here or use a special model just to support selection/expanding (see here).

Another weakness of the WPF TreeView is poor performance. It takes more the 10 seconds to expand a node containing 5000 subnodes! Even navigation becomes very slow — more than a second to just move a focus.


But there is an alternative way to display hierarchical data — use the ListView. The basic idea is the following:

  • For each tree node we create a row in the ListView.

  • The ListViewItem template contains a special control named RowExpander which allows you to expand/collapse a node and any number of additional controls to represent the data. Those controls are shifted to the right depending on the Node level.

  • When the TreeNode is expanded/collapsed we need to add/remove rows to/from the ListView

Using this approach we get all the benefits from ListView: multiselection, a possibility to display several columns, performance improvements comparing to TreeView, less memory usage because of virtualization support (it means that visual elements will be created only for the items currently displayed on the screen).


WPF TreeView is supposed to be used with HierarchicalDataTemplate where you specify a property containing child items. It's quite simple, but requires you to provide such a property in your business objects, which means that you can't display the hierarchy of simple types, like String, in the TreeView. Additionally, I prefer to use another approach — use a special interface which describes hierarchical data:

public interface ITreeModel
    /// Get list of children of the specified parent
    IEnumerable GetChildren(object parent);

    /// returns weather specified parent has any children or not.
    bool HasChildren(object parent);

The HasChildren method is used to display/hide the expander control without calling GetChildren, which can be time expensive. Note that items are loaded on demand in TreeListView, which means the GetChildren method will be called only when the corresponding parent node expands.

Realization Details

We create subclasses of ListView and ListviewItem:

public class TreeList: ListView
    /// Internal collection of rows representing visible nodes, actually displayed
    /// in the ListView
    internal ObservableCollectionAdv Rows
        private set;

    protected override DependencyObject GetContainerForItemOverride()
        return new TreeListItem();

    protected override bool IsItemItsOwnContainerOverride(object item)
        return item is TreeListItem;

When the Aaren node is collapsed/expanded we need to insert/remove all child nodes and notify ListView about that. As the system class ObservableCollection doesn't provide methods for that we need to create our own collection class:

public class ObservableCollectionAdv : ObservableCollection
    public void RemoveRange(int index, int count)
        var items = this.Items as List;
        items.RemoveRange(index, count);

    public void InsertRange(int index, IEnumerable collection)
        var items = this.Items as List;
        items.InsertRange(index, collection);

For every item created by the model we create a TreeNode class which will store node status (IsSelected, IsExpanded) and track changes in the model if it provides such information:

void ChildrenChanged(object sender, NotifyCollectionChangedEventArgs e)
    switch (e.Action)
    case NotifyCollectionChangedAction.Add:
        if (e.NewItems != null)
            int index = e.NewStartingIndex;
            int rowIndex = Tree.Rows.IndexOf(this);
            foreach (object obj in e.NewItems)
                Tree.InsertNewNode(this, obj, rowIndex, index);

    case NotifyCollectionChangedAction.Remove:
        if (Children.Count > e.OldStartingIndex)

    case NotifyCollectionChangedAction.Move:
    case NotifyCollectionChangedAction.Replace:
    case NotifyCollectionChangedAction.Reset:
        while (Children.Count > 0)
    HasChildren = Children.Count > 0;

Using the Code

The source code of the article contains two examples using TreeListView. One uses a classic TreeView style and the other displays how to interact with the TreeListView. The other shows how several columns can be used to display system registry.

Points of Interest

In the current implementation of the TreeListView you have to keep the XAML markup of the TreeListItem in the client library. Which means that you have to copy it to each project using the control. Normally this information should be stored in the same library as the control itself, but it just didn't work this way. If somebody finds the way how to achieve this, don't hesitate to share it.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Andrey Gliznetsov
Software Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

QuestionBinding Mode=TwoWay Pin
Member 1163564420-Feb-19 0:02
MemberMember 1163564420-Feb-19 0:02 
QuestionTrigger method when Expand/Collapse signs of a node is clicked? Pin
Abbas Mousavi29-Jul-18 23:03
MemberAbbas Mousavi29-Jul-18 23:03 
QuestionExport it ot Excel Pin
Narayan111119-Aug-17 1:21
MemberNarayan111119-Aug-17 1:21 
QuestionBinding IsExpanded ViewModel Pin
Axoud12-Apr-17 5:52
MemberAxoud12-Apr-17 5:52 
GeneralMy vote of 5 Pin
Maxim Gorki28-May-15 13:35
MemberMaxim Gorki28-May-15 13:35 
QuestionBinding to IsExpanded Property Pin
Member 803563420-Feb-15 1:03
MemberMember 803563420-Feb-15 1:03 
QuestionHow to modify value of GridViewColumn Pin
Rafał Niewiński23-Jan-15 10:47
MemberRafał Niewiński23-Jan-15 10:47 
GeneralMy vote of 5 Pin
ivailo bahchevanov18-Dec-14 13:14
Memberivailo bahchevanov18-Dec-14 13:14 
GeneralMy vote of 5 Pin
amitthk27-Oct-14 21:47
professionalamitthk27-Oct-14 21:47 
QuestionSorting capability -> Is there a way to add sorting capability to tree view columns? Pin
Member 797920026-Sep-14 8:42
MemberMember 797920026-Sep-14 8:42 
QuestionA way to scroll CurrentItem into View? Pin
v0id2416-Sep-14 7:29
Memberv0id2416-Sep-14 7:29 
QuestionIs it possible to place data in the non-leaf rows? Pin
RamonFHerrera13-Jun-14 14:08
MemberRamonFHerrera13-Jun-14 14:08 
QuestionFiltering Pin
looney_ops13-Mar-14 1:33
Memberlooney_ops13-Mar-14 1:33 
AnswerRe: Filtering Pin
Member 797920026-Sep-14 8:43
MemberMember 797920026-Sep-14 8:43 
Questionhow to Bind to SelectedNode Name property Pin
shakti saxena10-Mar-14 11:03
Membershakti saxena10-Mar-14 11:03 
QuestionBind IsSelected and IsExpanded properties to viewmodel Pin
Georgios Petrou22-Apr-13 9:38
MemberGeorgios Petrou22-Apr-13 9:38 
AnswerRe: Bind IsSelected and IsExpanded properties to viewmodel Pin
zeno.deluca2-Dec-13 3:29
Memberzeno.deluca2-Dec-13 3:29 
GeneralExcellent, very useful, my vote of 5! Pin
BGW21-Mar-13 0:38
MemberBGW21-Mar-13 0:38 
GeneralMy vote of 1 Pin
tbayart5-Mar-13 23:52
Membertbayart5-Mar-13 23:52 
QuestionRight click and double click Pin
Brad Yinger1-Feb-13 10:46
MemberBrad Yinger1-Feb-13 10:46 
QuestionHow to get the parent of a selected node. Pin
gnirujan3-Dec-12 20:50
Membergnirujan3-Dec-12 20:50 
AnswerRe: How to get the parent of a selected node. Pin
Member 89276567-Dec-12 14:04
MemberMember 89276567-Dec-12 14:04 
BugMEMORY LEAK question Pin
Tafroman13-Sep-12 12:38
MemberTafroman13-Sep-12 12:38 
Question+5 very interesting, Andrey Pin
BillWoodruff23-Aug-12 20:52
mveBillWoodruff23-Aug-12 20:52 
Questioncontacting the author Pin
Barbara Evans22-Aug-12 6:24
MemberBarbara Evans22-Aug-12 6:24 

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

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

Posted 5 Nov 2008


192 bookmarked