<Window x:Class="WpfDemo.HierarchicalDataTemplateDemo1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfDemo" mc:Ignorable="d" Title="HierarchicalDataTemplateDemo1" Height="300" Width="300"> <Window.Resources> <XmlDataProvider x:Key="ds" XPath="Data/Grade"> <x:XData> <Data xmlns=""> <Grade Name="一年级"> <Class Name="甲班"> <Group Name="A组"></Group> <Group Name="B组"></Group> <Group Name="C组"></Group> </Class> <Class Name="乙班"> <Group Name="A组"></Group> <Group Name="B组"></Group> <Group Name="C组"></Group> </Class> </Grade> <Grade Name="二年级"> <Class Name="甲班"> <Group Name="A组"></Group> <Group Name="B组"></Group> <Group Name="C组"></Group> </Class> <Class Name="乙班"> <Group Name="A组"></Group> <Group Name="B组"></Group> <Group Name="C组"></Group> </Class> </Grade> </Data> </x:XData> </XmlDataProvider> <!--年级模板--> <HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}"> <TextBlock Text="{Binding XPath=@Name}"></TextBlock> </HierarchicalDataTemplate> <!--班级模板--> <HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}"> <RadioButton Content="{Binding XPath=@Name}" GroupName="gn"></RadioButton> </HierarchicalDataTemplate> <!--小组模板--> <HierarchicalDataTemplate DataType="Group" > <CheckBox Content="{Binding XPath=@Name}"></CheckBox> </HierarchicalDataTemplate> </Window.Resources> <Grid> <TreeView ItemsSource="{Binding Source={StaticResource ds}}"></TreeView> </Grid> </Window>
TreeView 显示多层级,不同类型数据 层级式数据模板 HierarchicalDataTemplate XmlDataProvider 前台