WPF TreeView

The treeview control is always a bit difficult, I remember my first contact with Gtk.TreeView and it wasn't straightforward but the behaviour of WPF Treeview is still weird for me.
Basicly if you use a HierarchicalDataTemplate you won't be able to manage the TreeViewItems programmatically so better you create the whole treeview programmatically.
In order to go through the items in the tree, the LogicalTreeHelper and the VisualTreeHelper are very important classes. They are key classes for XAML in general.
The links below has been very useful to undertand that:

As you can read in the posts, to find out what item in the TreeView is at the position where the ContextMenu was opened, you should populate the tree with TreeViewItems, not with the HierarchicalDataTemplate.
When populating the treeview with TreeViewItems, use the Header property to attach the objects you really want to show in the tree. As usual, override the ToString property in such objects to display the desired content 😉

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://work.j832.com Kevin Moore

    Have you looked at my TreeView helper in the bag-o-tricks? (http://wpf.netfx3.com/files/folders/controls/entry10297.aspx)

    I’d love to know why you feel you need to create the TreeView items directly. In general, we *want* people to use data binding. If that isn’t working for you, I’d like to know why.

  • http://carlosble.com Carlos Ble

    Hi Kevin!
    It is very cool to get the people who develop WPF interested in their users, thanks for your comment.
    I haven’t seen your samples but I’ll do so today.
    For now, my problem is that given my treeview:


    <TreeView Width="173" x:Name="_treeView" Visibility="Collapsed">
    <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=MY_COLLECTION_1}">
    <TextBlock FontWeight="Bold" Text="{Binding Path=MyClass_0.Code}" />
    <HierarchicalDataTemplate.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=My_COLLECTION_2}">
    <TextBlock Text="{Binding Path=MyClass_1.Code}" />
    <HierarchicalDataTemplate.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=MY_COLLECTION_3}">
    <TextBlock Text="{Binding Path=MyClass_2.Code}" />
    <HierarchicalDataTemplate.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=My_COLLECTION_4}" >
    <TextBlock Text="{Binding Path=MyClass_3.Code}" />
    <HierarchicalDataTemplate.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=My_COLLECTION_5}">
    <TextBlock Text="{Binding Path=MyClass_4.Code}" />
    <HierarchicalDataTemplate.ItemTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding Path=MyClass_5.Code}" />
    </DataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
    </TreeView>

    I add a list of MyClass0 items to the treeview.Items property programmatically. MyClass0 has a collection of MyClass1 items and so forth. It works fine and I am able to retrieve the classes through the treeview.SelectedItem property, but I cannot find out which item is the parent of a given item in the tree, since the treeview only considers items the ones at the first level. Hope you know what I mean.
    The relationship between the XAML and the API seems to be not very tight.

  • http://carlosble.com Carlos Ble

    Your samples are great, I’ve learned few things looking at your code in special your TreeViewDataItem class but I still don’t see how to solve my program other than programmatically.
    If I understand correctly you have a tree based on the Children propery. The objects in the collections, they all have that property but what happens when the name of the collections are different for every class?

  • http://uggbootsuk14.blogujem.cz/note/88457/ugg.html ugg

    Good day it was dropped at my attention you will find there’s downside to your feed I do hope you make it better soon.