样式
1. DataTrigger和MultiDataTrigger可以实现任何触发器
DataTrigger多了一个Binding的属性
<Style x:Key="childStyle" TargetType="Control">
<Setter Property="Background" Value="BurlyWood"/>
<Style.Triggers>
<!-- 绑定当前的radio单选框,如果按钮选中,触发字体设置 -->
<DataTrigger Binding="{Binding ElementName=radio, Path=IsChecked}" Value="True">
<Setter Property="FontSize" Value="20"/>
</DataTrigger>
</Style.Triggers>
</Style>
<RadioButton Style="{StaticResource ResourceKey=childStyle}"
Name="radio" Content="我要变成20号字"></RadioButton>
模板
2. 模板分类4种都继承自FrameworkTemplate模板
____________________|____________________________
| | |
控件模板 条目容器模板 数据模板
(修改控件外观) (修改条目呈现方式) (修改数据显示的方式)
ControlTemplate ItemsPanelTempalte DataTemplate
_________________________|________
| |
原有模板的属性原封不动的投放到自定义模板中 分层数据模板
(绑定了控件Content属性,Text控件无) (一般TreeviewItem用的比较多)
ContentPresenter HierarhicalDataTemplate
3. ContentPresenter/ItemsPrresenter ----用来呈现内容控件的内容,它的ContentSource默认绑定控件的Content属性,如果在自定义控件中不添加<ContentPresent/>,它的内容将不会显示,只显示外观。
它也可以这样使用修改ContentSource
<HeaderedContentControl Header="Header" HeaderStringFormat="I'm {0}"
Content="Content" ContentStringFormat="I'm {0}">
<HeaderedContentControl.Template>
<ControlTemplate TargetType="HeaderedContentControl">
<DockPanel>
<ContentPresenter ContentSource="Header" DockPanel.Dock="Top"></ContentPresenter>
<!--等同于<ContentPresenter ContentSource="Content"/>-->
<ContentPresenter></ContentPresenter>
</DockPanel>
</ControlTemplate>
</HeaderedContentControl.Template>
</HeaderedContentControl>
<Style x:Key="{x:Type ItemsControl}"
TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
对于ItemsControl的自定义样式需要在控件外观模板中添加<ItemsPresenter/>
4. 模板也有Trigger属性功能和Style的Trigger一样
5. DataTemplate模板:ListBox-ComboBox-Listview等控件绑定数据模板显示的是该模板的Tosting()方法(默认为项目名.类名),可以通过重写该方法修改显示方式或者DisplayNamePath
注意DisplayNamePath是直接写数据源Item的属性名,不用加Binding,之前总是加{Binding PropertyName}无法实现效果,一度怀疑人生
<ListBox ItemsSource="{x:Static local:MainWindow.peoples}"/>
<ListBox ItemsSource="{x:Static local:MainWindow.persons}"/>
<ListBox ItemsSource="{x:Static local:MainWindow.persons}" DisplayMemberPath="Name"/>
<ComboBox ItemsSource="{x:Static local:MainWindow.persons}" DisplayMemberPath="Name"/>
public static List<Person> persons = new List<Person>() {
new Person() { Name = "Leo", Age = 23, Address = "山东" },
new Person() { Name = "Gina", Age = 22, Address = "背景" } };
public static List<People> peoples = new List<People>() {
new People() { Name = "Leo", Age = 23, Address = "山东" },
new People() { Name = "Gina", Age = 22, Address = "背景" } };
6. ItemsPanelTempalte用来描述ItemsControl的容器样式类如ListBox的ItemsPanelTemplate是一个VisualizingStackPanel里面装了一个StackPanel。如果改成WrapPanel
<ListBox ItemsSource="{x:Static local:MainWindow.peoples}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Width="120" Height="40"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
7. HierarhicalDataTemplate分层数据模板
<XmlDataProvider x:Key="Info" XPath="Nations">
<x:XData>
<Nations xmlns="">
<Nation Name="中国">
<Provinces>
<Province Name="安徽">
<Citys>
<City Name="安庆">
<Countrys>
<Country Name="潜山"/>
<Country Name="桐城"/>
</Countrys>
</City>
<City Name="合肥">
<Countrys>
<Country Name="长丰"/>
<Country Name="肥东"/>
</Countrys>
</City>
</Citys>
</Province>
<Province Name="江苏">
<Citys>
<City Name="南京">
<Countys>
<Country Name="溧水"/>
<Country Name="高淳"/>
</Countys>
</City>
<City Name="苏州">
<Countys>
<Country Name="常熟"/>
</Countys>
</City>
</Citys>
</Province>
</Provinces>
</Nation>
</Nations>
</x:XData>
</XmlDataProvider>
<HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}">
<StackPanel Background="AliceBlue">
<TextBlock FontSize="20" Text="{Binding XPath=@Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Province" ItemsSource="{Binding XPath=Citys/City}">
<StackPanel Background="LightBlue">
<TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="City" ItemsSource="{Binding XPath=Countrys/Country}">
<StackPanel Background="LightBlue">
<TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Country">
<StackPanel Background="LightSalmon">
<TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation}"></TreeView>