一、ControlTemplate
算法内容的表现形式,一个控件怎样组织其内部结构才能让他更符合业务逻辑,是指控件外衣。
二、DataTemplate
数据内容的表现形式,是数据的外衣。常用在三处:ContentTemplate,ItemTemplate和CellTemplate
三、Style的介绍
Style用来给控件设计多种外观样式和行为样式,主要由Setter(设置器)和Trigger(触发器)两种元素组成。Setter帮助我们设置控件的静态外观,Trigger(触发器,满足条件时触发一个行为)帮助我们设置控件的行为风格。
1. Setter的使用
<Window.Resource> <Style TargetType="TextBlock"> <Setter Property="FontSize" value="24"/> </Style> </Window>
设置控件的ControlTemplate,把Setter的Property设置为Template 并给Value提供一个ControlTemplate。
<Window.Resources>
<Style x:Key="IsMouseOver">
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="true">
<!-- 当鼠标移过按钮时,触发样式变更-->
<Setter Property="Control.FontStyle" Value="Italic"/>
<Setter Property="Control.Background" Value="Blue"/>
<Setter Property="Control.FontSize" Value="18"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="ButtonStyleLogin" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal">
<Image x:Name="minBtn" Source="Point1.png" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="Point2.png" TargetName="minBtn" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Source" Value="Point3.png" TargetName="minBtn" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="137,201,0,0" VerticalAlignment="Top" Width="218" Height="58" Style="{StaticResource ButtonStyleLogin}"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="515,201,0,0" VerticalAlignment="Top" Width="175" Height="58" Style="{StaticResource IsMouseOver}"/>
</Grid>
2.Trigger的使用
(1)基本Trigger
由Property(关注的属性名称) Value(触发条件)和Setters(一组Setter)三个属性组成
<Style x:Key="CheckBoxStyle" TargetType="CheckBox"> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Trigger.Setters> <Setter Property="FontSize" Value="20"/> <Setter Property="Foreground" Value="Orange"/> </Trigger.Setters> </Trigger> </Style.Triggers> </Style>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="235,293,0,0" VerticalAlignment="Top" Height="23" Width="86" Style="{StaticResource CheckBoxStyle}"/>
(2)MultiTrigger:多条件同时成立才会被触发,比Trigger多了一个Condition属性
<Style x:Key="CheckBoxStyle" TargetType="CheckBox"> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsChecked" Value="true"/> <Condition Property="Content" Value="yxz"/> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="FontSize" Value="20"/> <Setter Property="Foreground" Value="Red"/> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers> </Style>
(3)DataTrigger
(4)MultiDataTrigger多数据条件触发器
(5)EventTrigger
由事件触发,执行的是一段动画,UI层的动画效果一般和它有关。
<Style TargetType="Button" x:Key="ButtonStyle"> <Style.Triggers> <EventTrigger RoutedEvent="MouseEnter"> <BeginStoryboard> <Storyboard> <DoubleAnimation To="15" Duration="0:0:0:2" Storyboard.TargetProperty="Width"/> <DoubleAnimation To="15" Duration="0:0:0:2" Storyboard.TargetProperty="Height"/> </Storyboard> </BeginStoryboard> </EventTrigger> <EventTrigger RoutedEvent="MouseLeave"> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0:2" Storyboard.TargetProperty="Width"/> <DoubleAnimation Duration="0:0:0:2" Storyboard.TargetProperty="Height"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Style.Triggers> </Style>
总结:触发器不仅仅存在与Style中,各种Template也拥有自己的触发器。