Virtualizing Treeview – aka TreeListBox v2.0

 As promised this weekend I re-wrote the TreeListBox (previous article posted here) control to support better virtualization… Basically I changed the whole idea… There are the same properties and same interface so whoever was using the old TreeListBox can just make an update and everything should work properly.

What was wrong in the old implementation was that I was generating the TreeListBoxItems instead of letting the VirtualizingStackPanel to generate them; the VirtualizingStackPanel was only not adding them to the logical tree! I realized this when I was profiling the component for memory.

In the new implementation I changed the whole concept. Basically now I generate a list of TreeListBoxInfo object from the HierarchalItemsSource specified by the user and set it to the ItemsSource of the TreeListBox.

The object transformation is something like the following

List of Persons  – > treelistbox.HierarchalItemsSource -> List of TreeListBoxInfo – > treelistBox.ItemsSource

Basically the TreeListBoxInfo is just an intermediate object that contains information such as the Level, Children and of course it stores the actual data item (in the example above the data item would be the person object)

Ok, so we have a flat list now but if I set the ItemsSource to have a list of TreeListBoxInfo this would mean that all DataTemplates for the TreeListBoxItem would not work because the user doesn’t know that the Items in the ItemsSource are of type TreeListBoxInfo! So here what we do is override the PrepareContainerForItemOverride method. This method allows us to change the object to set for the TreeListBoxItem and so we do the following…

/// <summary>
/// Prepares the new VirtualizingTreeViewItem with the actual business object
/// </summary>
/// <param name=”element”>The element (VirtualizingTreeViewItem) to apply the template</param>
/// <param name=”item”>The business object to set</param>
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
            TreeListBoxItem listItem = element as TreeListBoxItem;
            TreeListBoxInfo actualItem = item as TreeListBoxInfo;
            //prepares the item with the relative VirtualizingTreeViewInfo
            //pass the actual data item instead of the  VirtualizingTreeViewInfo
            base.PrepareContainerForItemOverride(element,   actualItem.DataItem);

As you can see we replace the item to prepare with the DataItem of the TreeListBoxInfo.

There is a lot of other things going on in the background such as registering to data binding events for children and finding the correct index where to push a new item in the list but I think that this is all you need to start working!

Please send me an email at if you find any bugs or even if you have any suggestions.

Thanks for your support !



[Last Update of Source Code – 06 October 2007 – fixed some memory leaks]

11 thoughts on “Virtualizing Treeview – aka TreeListBox v2.0

  1. Pingback: Virtualizing Treeview - aka TreeListBox « C# Disciples

  2. Hi Marlon, Thank you so much for this control! It is a life saver for me 🙂

    I have a question: Is it possible for me to use an XML file as a datasource here?

  3. 🙂 I can? Was looking through the Avalon Test application, and you had a TreeStructure class, so I was not sure if I had to convert the Xaml into a similar class in order to use your control.

    Great control once again! 😀

  4. Hi Marlon,

    I’m kinda stuck with using a XML file. If my XML file has a hierarchical structure, example:

    how do i define it to the tree? Since a Listview does not support a HierarchicalDataTemplate.

    Thanks for all your help so far!


  5. Hi Marlon,
    I’m using the TreeListBox, sorry for all the confusion.. 😦 I’ve managed to get my hierarchical xml data displayed 🙂 I need your help on another issue
    – I have an event which is raised when a XmlNode ID is retrieved, i then have to select the corresponding TreeNode in the TreeListBox (and this might be the ID of an unexpanded node :S). Any ideas as to how I should approach this selection process?

    Please feel free to contact me via my email address! Thank you so much for your help.


  6. Hi Marlon,

    I have a question regarding the items out of view.
    How can I expand an item out of view?
    Thank you for your time.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s