Can I bind a WPF TreeView to a single root node?

Viewed 6982

Say I have a binary tree, where the root of the data structure is just a tree node. For each node, the children are accessible through the Children property. Here's what I tried. The TreeRoot is a property of the inherited data context, but it's a single node (not a collection).

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="TreeNodeTemplate" ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
</UserControl.Resources>

<Grid>
    <TreeView ItemsSource="{Binding TreeRoot}" ItemTemplate="{StaticResource TreeNodeTemplate}" />
</Grid>
3 Answers

I think your problem is that your hierarchical data template is only applied to the root node, so you never see anything past the root's children. Try this instead:

<UserControl.Resources>
    <HierarchicalDataTemplate DataType="{x:Type TreeNode}" ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
</UserControl.Resources>

<Grid>
    <TreeView ItemsSource="{Binding TreeRoot}"/>
</Grid>

Where TreeNode is the name of your tree node class.

Like Aviad said, if you use DataType="{x:Type TreeNode}" then the TreeView will automatically use that template for any object of that type.

If you have multiple types in your hierarchy, you can specify multiple HierarchicalDataTemplates each with the data type it's for. That would let the TreeView handle each type differently.

<HierarchicalDataTemplate DataType="{x:Type TreeNode}" ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type TreeLeaf}">
    <TextBlock Text="{Binding Message}" Background="Red" />
</HierarchicalDataTemplate>
Related