关于wpf的触发器总共三大类:Trigger、DataTrigger(没弄过,主要是数据绑定,不解释)、EventTrigger(没弄过,主要是动画渲染,不解释)
1.Trigger
Trigger也叫简单触发器,主要是用来控制样式渲染用的,和css3功能及其相似。
Trigger的单条件触发器案例:
不适用新控件案例: <Style x:Key="UInfoBCss" TargetType="Border"> <Style.Triggers> <!--鼠标移上去变色,离开使value=false即可--> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="BorderBrush" Value="#96c4ee"></Setter> </Trigger> </Style.Triggers> </Style> 使用新控件案例: <Style x:Key="EHCBtnECss" TargetType="Button" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <StackPanel> <Image x:Name="CBImg" Source="Images\按钮.png" Height="45" Margin="0,0,0,0"></Image> <Label Width="50" Height="45" Margin="0,-45,0,0" Block.TextAlignment="Center" Padding="0,6,0,0" Content="Cancel"></Label> </StackPanel> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="CBImg" Property="Source" Value="Images\按钮经过.png"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
Trigger的多条件(MultiTrigger)触发器案例:
<Style x:Key="EHCloseCssF" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Image x:Name="EHTmg" Source="Images\关闭提示.png"></Image> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="true"></Condition> <Condition Property="IsFocused" Value="true"></Condition> </MultiTrigger.Conditions> <Setter TargetName="EHTmg" Property="Source" Value="Images\关闭提示_悬停.png"></Setter> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
//