C# Disciples

my life in Avalon ….

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
            listItem.PrepareItem(actualItem);
            //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 marlongrech@gmail.com if you find any bugs or even if you have any suggestions.

Thanks for your support !

DOWNLOAD SOURCE HERE

Regards

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

October 1, 2007 - Posted by marlongrech | .Net 3.0, WPF, WPF Custom Controls | | 11 Comments

11 Comments »

  1. [...] Virtualizing Treeview – aka TreeListBox Before you start reading - I have a new version of the  TreeListBox available here [...]

    Pingback by Virtualizing Treeview - aka TreeListBox « C# Disciples | October 2, 2007 | Reply

  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?

    Comment by Serene | April 17, 2008 | Reply

  3. Yea sure you can…. you can use the XmlDataProvider just like in any other WPF Control :D

    Comment by marlongrech | April 17, 2008 | Reply

  4. :) 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! :D

    Comment by Serene | April 17, 2008 | Reply

  5. Glad you like it… if you have any problems in the future let me know…

    P.S nice site… it made me feel hungry…

    Comment by marlongrech | April 17, 2008 | Reply

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

    Serene

    Comment by Serene | April 18, 2008 | Reply

  7. Hi there,

    Are you using a listview or the TreeListBox?
    I’m confused…sorry :(

    Regards

    Comment by Marlon Grech | April 18, 2008 | Reply

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

    Serene

    Comment by Serene | April 22, 2008 | Reply

  9. Hi Marlon,
    I created a Virtualizing TreeView like yours. Please have a look http://www.codeproject.com/KB/tree/VTreeView.aspx

    Comment by Paul | June 20, 2008 | Reply

  10. Hi Marlon,

    Could you please tell me how to use “Expand All” and “Collapse All” in your vitual tree listview.

    Thanks!

    Comment by svarn | September 3, 2008 | Reply

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

    Alberto

    Comment by Alberto | December 4, 2008 | Reply


Leave a comment