[WPF Series]-Data bonding datatemplate support for tiered data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.