WPF TreeView Binding

33,405

The reason why this isn't working is that you are only specifying the DataTemplate for the TreeView. Since the TreeViewItems that it generates are also ItemsControls, they would need to have the ItemTemplate set as well.

The easiest way to achieve what you are hoping for is to put the HierarchicalDataTemplate in the resources of the TreeView (or any of its parent visuals), and set the DataType of the HierarchicalDataTemplate so it is applied to all of your items.

In your container's declaration (most likely window), you need to define a mapping to the namespace where page is defined.

e.g.

<Window ...
    xmlns:local="clr-namespace:NamespaceOfPageClass;assembly=AssemblyWherePageIsDefined">

<TreeView Margin="12" Name="TreeViewPages" ItemsSource="{Binding}" />
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType=”{x:Type local:Page}” ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Path=ShortTitle}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
Share:
33,405
Zack Peterson
Author by

Zack Peterson

Specializes in the design and creation of web and desktop applications. Contributes in all aspects of the software development process such as: requirements analysis and product definition; prototyping; choosing architecture and framework; interface design; database design; installation and integration; documentation and training; gathering feedback; and maintenance.

Updated on August 12, 2022

Comments

  • Zack Peterson
    Zack Peterson over 1 year

    I've got a class with Parent and Children properties.

    ADO.NET Entity Framework Hierarchical Page Class http://img148.imageshack.us/img148/6802/edmxxe8.gif

    I want to display this hierarchy in a WPF treeview.

    Here's my XAML...

    <TreeView Margin="12" Name="TreeViewPages" ItemsSource="{Binding}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Path=ShortTitle}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
    

    And my VB code...

    Dim db As New PageEntities
    Dim t = From p In db.Page.Include("Children") _
            Where p.Parent Is Nothing _
            Select p
    TreeViewPages.ItemsSource = t
    

    But then I only get a tree two levels deep. What do I need to do to get this working?

  • Zack Peterson
    Zack Peterson over 15 years
    DataType="{x:Type local:Page}" gives errors: (1) "Type 'Page' was not found." and (2) "'local' is an undeclared namespace."
  • Abe Heidebrecht
    Abe Heidebrecht over 15 years
    Sorry about the confusion, I have updated the answer to clarify that you need to add an xmlns declaration to specify where to find the Page class.
  • Zack Peterson
    Zack Peterson over 15 years
    Thank you. I added xmlns:local="clr-namespace:PageManager" and it's templating the nodes on the tree now. But I still only get a tree two levels deep.
  • Abe Heidebrecht
    Abe Heidebrecht over 15 years
    This should work... Are the objects in the Children collection the same type? Also, are you sure that your child objects have their Children populated?