So far, we've only discussed how to bind and display a single collection. At some point, the collection that you want to bind contains other collections. The Hierarchicaldatatemplate class is dedicated to the HeaderedItemsControl type to display such data.
Example Demo
In the following example, Listleaguelist is a list of league objects. Each league object has a collection of Name and Division objects. Each division has a collection of name and team objects, and each team object has a name.
<Windowx:class= "Sdksample.window1"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "Hierarchicaldatatemplate Sample"xmlns:src= "Clr-namespace:sdksample"> <DockPanel> <dockpanel.resources> <src:listleaguelistx:key= "MyList"/> <hierarchicaldatatemplateDataType= "{x:type Src:league}"ItemsSource= "{Binding Path=divisions}"> <TextBlockText="{Binding Path=name}"/> </hierarchicaldatatemplate> <hierarchicaldatatemplateDataType= "{x:type Src:division}"ItemsSource= "{Binding Path=teams}"> <TextBlockText="{Binding Path=name}"/> </hierarchicaldatatemplate> <DataTemplateDataType="{x:type Src:team}"> <TextBlockText="{Binding Path=name}"/> </DataTemplate> </dockpanel.resources> <MenuName= "Menu1"DockPanel.Dock= "Top"Margin= "10,10,10,10"> <MenuItemHeader= "My Soccer Leagues"ItemsSource="{Binding Source={staticresource MyList}}" /> </Menu> <TreeView> <TreeViewItemItemsSource="{Binding Source={staticresource MyList}}"Header= "My Soccer Leagues" /> </TreeView> </DockPanel></Window>
The example demonstrates that by using hierarchicaldatatemplate, you can easily display list data that contains other lists. The effect shows:
Reference
[Common Considerations for WPF series]-treeview
[WPF Series]-Data bonding datatemplate support for tiered data