A HierarchicalDataTemplate
is a way of saying ‘this is how you render this type of object and here is a property that can be probed to find the child nodes under this object’
Therefore you need a single property that returns the ‘children’ of this node.
e.g. (If you can’t make both Group and Entry derive from a common Node type)
public class Group{ ....
public IList<object> Items
{
get
{
IList<object> childNodes = new List<object>();
foreach (var group in this.SubGroups)
childNodes.Add(group);
foreach (var entry in this.Entries)
childNodes.Add(entry);
return childNodes;
}
}
Next you don’t need a HierarchicalDataTemplate
for entry since an entry doesn’t have children. So the XAML needs to be changed to use the new Items property and a DataTemplate
for Entry:
<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Entry}" >
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
And here’s what that looks like.