Having HierarchicalDataTemplates in a TreeView

前端 未结 1 1503
青春惊慌失措
青春惊慌失措 2020-12-02 23:17

With regards to a question I posted earlier on (WPF: Correctly storing an object in a TreeViewItem)

Is it possible to have nested Hierarchical

相关标签:
1条回答
  • 2020-12-02 23:36

    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>
    
    0 讨论(0)
提交回复
热议问题