Having HierarchicalDataTemplates in a TreeView

You dont need a nested template here, since TreeView control will take care of nesting it based on the DataType it requires. So just define Two HierarchicalDataTemplates for Album and Artist Type and one ordinary DataTemplate for your Track class.

   <HierarchicalDataTemplate  DataType="{x:Type local:Artist}" ItemsSource="{Binding Albums}" >          
         <TextBlock Text="{Binding Name}"/>                 
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate  DataType="{x:Type local:Album}" ItemsSource="{Binding Tracks}" >
        <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>        
    <DataTemplate DataType="{x:Type local:Track}">
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>

Leave a Comment