触发器能够在改变属性值的时候,根据值变化执行操作,在不需要创建一个新的控件的情况下,可以动态的改变控件的外观,当条件满足时,触发器可以改变任何属性的值,触发器通常定义在Style中,在窗体的根节点下被应用到指定的控件
Property Triggers
在Triggers集合中一个Trigger对象,当Trigger中定义的条件满足时,则会触发相应的操作,下面例子当鼠标移动到按钮上时,鼠标会变成手状态
<Window x:Class="Example_07.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Example_07" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"><Window.Resources><Style TargetType="Button"><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Cursor" Value="Hand" /></Trigger></Style.Triggers></Style></Window.Resources><Grid><Button Width="200" Height="100" Grid.Row="0" Grid.ColumnSpan="2">Hello World</Button></Grid>
</Window>
Data Triggers
Data Triggers 表示当绑定的数据满足条件时,会执行相应操作,这里我们先做一个简单的DataTrigger演示,当我们选中CheckBox时,会改变对应TextBlock中的值
<Window x:Class="Example_07.DataTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Example_07" mc:Ignorable="d" Title="DataTriggerSample" Height="200" Width="300"><Window.Resources><Style TargetType="TextBlock"><Style.Triggers><DataTrigger Binding="{Binding ElementName=cbIsPass,Path=IsChecked}" Value="True"><Setter Property="Text" Value="Pass"></Setter><Setter Property="Foreground" Value="Green"></Setter><Setter Property="FontSize" Value="40"></Setter></DataTrigger><DataTrigger Binding="{Binding ElementName=cbIsPass,Path=IsChecked}" Value="False"><Setter Property="Text" Value="NO"></Setter><Setter Property="Foreground" Value="Red"></Setter><Setter Property="FontSize" Value="40"></Setter></DataTrigger></Style.Triggers></Style></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition Height="20"></RowDefinition><RowDefinition Height="100"></RowDefinition></Grid.RowDefinitions><CheckBox Grid.Row="0" Name="cbIsPass" Content="是否通过" Width="100" Height="30"></CheckBox><TextBlock Grid.Row="1" TextAlignment="Center"></TextBlock></Grid>
</Window>
Event Triggers
事件触发器表示当指定的事件触发时,会执行对应的操作,事件触发器一般应用在动画中,下面例子演示鼠标移动到控件上时,字体会变大,当鼠标离开时,字体会恢复原始状态
<Window x:Class="Example_07.EventTriggerSample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Example_07" mc:Ignorable="d" Title="EventTriggerSample" Height="450" Width="800"><Window.Resources><Style TargetType="TextBlock"><Style.Triggers><EventTrigger RoutedEvent="MouseEnter"><EventTrigger.Actions><BeginStoryboard><Storyboard><DoubleAnimation Duration="0:0:3" Storyboard.TargetProperty="FontSize" To="100"/></Storyboard></BeginStoryboard></EventTrigger.Actions></EventTrigger><EventTrigger RoutedEvent="MouseLeave"><EventTrigger.Actions><BeginStoryboard><Storyboard><DoubleAnimation Duration="0:0:3" Storyboard.TargetProperty="FontSize" To="18" /></Storyboard></BeginStoryboard></EventTrigger.Actions></EventTrigger></Style.Triggers></Style></Window.Resources><StackPanel VerticalAlignment="Center"><TextBlock Text="事件触发器" TextAlignment="Center"></TextBlock></StackPanel>
</Window>
我们介绍了WPF常用的三种类型触发器Trigger、DataTrigger、EventTrigger另外还有两种多条件触发器MultiTrigger、MultiDataTrigger满足多个条件是触发