HierarchicalDataTemplate层次模板绑定XML数据源到Menu和TreeView

<Window x:Class="HierarchicalDataTemplate.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="300" Width="300"> <Window.Resources> <!-- XPath语法: / 从根节点选取    // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置    . 选取当前节点    .. 选取当前节点的父节点    @ 选取属性--> <XmlDataProvider x:Key="xdp" XPath="/Ball/League"> <x:XData> <Ball xmlns=""> <League Name="American League"> <Division Name="East"> <Team Name="凯尔特人"></Team> <Team Name="公牛"></Team> <Team Name="热火"></Team> </Division> <Division Name="Center"> <Team Name="森林狼"></Team> <Team Name="76人"></Team> </Division> <Division Name="West"> <Team Name="马刺"></Team> <Team Name="小牛"></Team> </Division> </League> <League Name="National League"> <Division Name="East"> <Team Name="中国队"></Team> <Team Name="韩国队"></Team> <Team Name="日本队"></Team> </Division> <Division Name="Center"> <Team Name="科威特队"></Team> <Team Name="伊拉克队"></Team> </Division> <Division Name="West"> <Team Name="印度队"></Team> <Team Name="埃及队"></Team> </Division> </League> </Ball> </x:XData> </XmlDataProvider> <HierarchicalDataTemplate DataType="League" ItemsSource="{Binding XPath=Division}"><!--这里也可以写成XPath=./Division ,表示当前League节点下的Division,而不会取到其他League下的Division--> <TextBlock Text="{Binding XPath=@Name}"></TextBlock> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="Division" ItemsSource="{Binding XPath=Team}"> <TextBlock Text="{Binding XPath=@Name}"></TextBlock> </HierarchicalDataTemplate> <DataTemplate DataType="Team"> <TextBlock Text="{Binding XPath=@Name}"></TextBlock> </DataTemplate> </Window.Resources> <Grid> <StackPanel> <Menu Name="menu1" DockPanel.Dock="Top" Margin="10,10,10,10"> <MenuItem Header="Menu"> <MenuItem Name="mi1" Header="Major League Baseball" ItemsSource="{Binding Source={StaticResource xdp}}" /> </MenuItem> </Menu> <TreeView ItemsSource="{Binding Source={StaticResource xdp}}" Name="myTreeView" /> </StackPanel> </Grid> </Window>

你可能感兴趣的:(xml,header,Class,menu,binding,DataTemplate)