WPF中的DataTemplate
<Window x:Class="DateTemplate应用.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DateTemplate应用" xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <!--DataTemplate--> <DataTemplate DataType="{x:Type local:Unit}"> <Grid> <StackPanel Orientation="Horizontal"> <Grid> <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"/> <TextBox Text="{Binding Year}"/> </Grid> <TextBlock Text="{Binding Price}" Margin="5,0"/> </StackPanel> </Grid> </DataTemplate> <!--数据源--> <collections:ArrayList x:Key="ds"> <local:Unit Year="2001" Price="100"/> <local:Unit Year="2002" Price="120"/> <local:Unit Year="2003" Price="140"/> <local:Unit Year="2004" Price="160"/> <local:Unit Year="2006" Price="180"/> <local:Unit Year="2008" Price="200"/> </collections:ArrayList> </Window.Resources> <StackPanel> <ListBox ItemsSource="{StaticResource ds}"/> <ComboBox ItemsSource="{StaticResource ds}"/> </StackPanel> </Window>
以XML数据格式来显示
<Window x:Class="XMLDataTemplate.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xmlDataTemplate="clr-namespace:XMLDataTemplate" Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen"> <Window.Resources> <!--DataTemplate--> <DataTemplate x:Key="MyXmlDataTemplate"> <Grid> <StackPanel Orientation="Horizontal"> <Grid> <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding XPath=@Price}"/> <TextBlock Text="{Binding XPath=@Year}"/> </Grid> <TextBlock Text="{Binding XPath=@Price}" Margin="5,0"/> </StackPanel> </Grid> </DataTemplate> <!--数据源--> <XmlDataProvider x:Key="Ds" XPath="Units/Unit"> <x:XData> <Units xmlns=""> <Unit Year="2001" Price="100"/> <Unit Year="2002" Price="120"/> <Unit Year="2001" Price="140"/> <Unit Year="2001" Price="160"/> <Unit Year="2001" Price="180"/> <Unit Year="2001" Price="200"/> <Unit Year="2001" Price="220"/> </Units> </x:XData> </XmlDataProvider> </Window.Resources> <StackPanel> <ListBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/> <ComboBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/> </StackPanel> </Window>
显示层级数据的模板是HierarchicaDataTemplate
<?xml version="1.0" encoding="utf-8" ?> <Data xmlns=""> <Grade Name="一年级"> <Class Name="1班"> <Group Name="A组"/> <Group Name="B组"/> <Group Name="C组"/> <Group Name="D组"/> </Class> </Grade> <Grade Name="二年级"> <Class Name="1班"> <Group Name="A组"/> <Group Name="B组"/> <Group Name="C组"/> <Group Name="D组"/> </Class> </Grade> <Grade Name="三年级"> <Class Name="1班"> <Group Name="A组"/> <Group Name="B组"/> <Group Name="C组"/> <Group Name="D组"/> </Class> </Grade> </Data>
<Window x:Class="TreeView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <!--数据源--> <XmlDataProvider x:Key="ds" Source="Data.xml" XPath="Data/Grade"/> <!--年级模板--> <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" ItemsSource="{Binding XPath=Student}"> <CheckBox Content="{Binding XPath=@Name}"></CheckBox> </HierarchicalDataTemplate> </Window.Resources> <Grid> <TreeView ItemsSource="{Binding Source={StaticResource ds}}"></TreeView> </Grid> </Window>