views:

8420

answers:

1
+3  A: 

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>
Abe Heidebrecht
DataType="{x:Type local:Page}" gives errors: (1) "Type 'Page' was not found." and (2) "'local' is an undeclared namespace."
Zack Peterson
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.
Abe Heidebrecht
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.
Zack Peterson
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?
Abe Heidebrecht