决定控件外观的是ControlTemplate,决定数据外观的是DataTemplate。
把ControlTemplate应用在所有目标上需要借助Style来实现,Style不能标记x:Key,例如:
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" SnapsToDevicePixels="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="5">
<ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Margin" Value="5" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Height" Value="25" />
</Style>
</Window.Resources>
<Window.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="1,0">
<GradientStop Color="#FF00B0FF" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Window.Background>
<StackPanel>
<TextBox />
<TextBox />
<TextBox Style="{x:Null}" Margin="5" />
</StackPanel>
效果图
把DataTemplate应用在某个数据类型上的方法是设置DataTemplate的DataType属性,并且DataTemplate作为资源是也不能带有x:Key标记:
<Window.Resources>
<DataTemplate DataType="{x:Type localEntity:Unit}">
<Grid>
<StackPanel Orientation="Horizontal">
<Grid>
<Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}" />
<TextBlock Text="{Binding Year}" />
</Grid>
<TextBlock Text="{Binding Price}" Margin="5,0" />
</StackPanel>
</Grid>
</DataTemplate>
<!--数据源-->
<c:ArrayList x:Key="ds">
<localEntity:Unit Year="2001年" Price="100" />
<localEntity:Unit Year="2002年" Price="120" />
<localEntity:Unit Year="2003年" Price="140" />
<localEntity:Unit Year="2004年" Price="160" />
<localEntity:Unit Year="2005年" Price="180" />
<localEntity:Unit Year="2006年" Price="200" />
</c:ArrayList>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{StaticResource ds}" />
<ComboBox ItemsSource="{StaticResource ds}" />
</StackPanel>
public class Unit
{
public int Price { get; set; }
public string Year { get; set; }
}
DataTemplate还可以读取XML文件中的数据,首先准备Data.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Data xmlns="">
<Grade Name="一年级">
<Class Name="甲班">
<Group Name="A 组" />
<Group Name="B 组" />
<Group Name="C 组" />
</Class>
<Class Name="乙组">
<Group Name="A 组" />
<Group Name="B 组" />
<Group Name="C 组" />
</Class>
</Grade>
<Grade Name="二年级">
<Class Name="甲班">
<Group Name="A 组" />
<Group Name="B 组" />
<Group Name="C 组" />
</Class>
<Class Name="乙组">
<Group Name="A 组" />
<Group Name="B 组" />
<Group Name="C 组" />
</Class>
</Grade>
</Data>
XAML为:
<Window.Resources>
<!--数据源-->
<XmlDataProvider x:Key="da" Source="Data.xml" XPath="Data/Grade" />
<!--年纪模板-->
<HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}">
<TextBlock Text="{Binding XPath=@Name}" />
</HierarchicalDataTemplate>
<!--班级模板-->
<HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}">
<RadioButton Content="{Binding XPath=@Name}" GroupName="gn" />
</HierarchicalDataTemplate>
<!--小组模块-->
<HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Student}">
<CheckBox Content="{Binding XPath=@Name}" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="5" ItemsSource="{Binding Source={StaticResource da}}"/>
</Grid>
效果图: