当达到了出发的条件,执行设定的响应,可以是样式、数据变化、动画等。触发器的类型有:
Trigger:检测依赖属性的变化,触发器生效
<Window.Resources> <Style x:Key="TestStyle" TargetType="Button"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Red" /> </Trigger> <Trigger Property="IsMouseOver" Value="False"> <Setter Property="Foreground" Value="Blue" /> </Trigger> </Style.Triggers> </Style> </Window.Resources>
MultiTrigger:多个条件的设置、达到满足条件、触发器生效
<Window.Resources> <Style x:Key="TestStyle" TargetType="Button"> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="true"/> <Condition Property="IsFocused" Value="true"/> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="FontSize" Value="20"/> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers> </Style> </Window.Resources>
DataTrigger:数据发生变化,触发器生效
<Window.Resources> <Style x:Key="TestStyle" TargetType="TextBox"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self},Path=Text}" Value="测试"> <Setter Property="Background" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> </Window.Resources>
MultiDataTrigger:多个数据发生变化,触发器生效
<Window.Resources> <Style x:Key="TestStyle" TargetType="TextBox"> <Setter Property="Background" Value="Pink" /> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}" Value="测试" /> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsFocused}" Value="true" /> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter Property="Background" Value="Red" /> <Setter Property="FontSize" Value="20" /> </MultiDataTrigger.Setters> </MultiDataTrigger> </Style.Triggers> </Style> </Window.Resources>
EventTrigger:事件触发器,触发了某类事件,触发器生效
<Window.Resources> <Style x:Key="TestStyle" TargetType="Button"> <Setter Property="Background" Value="Pink" /> <Style.Triggers> <EventTrigger RoutedEvent="MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <ColorAnimation To="Purple" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> </Style> </Window.Resources>
事件触发器有些不同的是,它触发执行的是一段动画,并且是通过RoutedEvent来执行要监视的事件
标签:动画,触发器,上位,事件,生效,WPF From: https://www.cnblogs.com/appform/p/17605353.html