WPF学习笔记-触发器
一、简单触发器
可为任何依赖项属性关联简单触发器。例如,可通过响应Control类的IsFocused、IsMouseOver以及IsPressed属性的变化,创建鼠标悬停效果和焦点效果。
1 <Window x:Class="WPFdemo12.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WPFdemo12" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Window.Resources> 10 <Style x:Key="Style"> 11 <Style.Triggers> 12 <Trigger Property="Control.IsFocused" Value="True"> 13 <Setter Property="Control.Background" Value="Red" /> 14 </Trigger> 15 </Style.Triggers> 16 </Style> 17 </Window.Resources> 18 <Grid> 19 <WrapPanel> 20 <Button Content="button1" Height="20" Width="500" Margin="3" Style="{StaticResource Style}"></Button> 21 <Button Content="button2" Height="20" Width="500" Margin="3"></Button> 22 </WrapPanel> 23 </Grid> 24 </Window>
当触发多个触发器,改同一个属性时,最后一个触发器修改属性有效。
二、多条件触发器
有时候触发的条件不止一个,要满足多个时候,就需要多条件触发
1 <Window x:Class="WPFdemo12.Window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WPFdemo12" 7 mc:Ignorable="d" 8 Title="Window1" Height="300" Width="300"> 9 <Window.Resources> 10 <Style x:Key="Styled"> 11 <Style.Triggers> 12 13 <MultiTrigger> 14 <MultiTrigger.Conditions> 15 <Condition Property="Control.IsFocused" Value="True"/> 16 <Condition Property="Control.IsMouseOver" Value="True"/> 17 </MultiTrigger.Conditions> 18 <MultiTrigger.Setters> 19 <Setter Property="Control.Foreground" Value="Red" /> 20 </MultiTrigger.Setters> 21 </MultiTrigger> 22 </Style.Triggers> 23 </Style> 24 </Window.Resources> 25 <Grid> 26 <WrapPanel> 27 <Button Content="button1" Height="20" Width="500" Margin="3" Style="{StaticResource Styled}"></Button> 28 <Button Content="button2" Height="20" Width="500" Margin="3"></Button> 29 </WrapPanel> 30 </Grid> 31 </Window>
三,事件触发器
用于处理动画
1 <Window x:Class="WPFdemo12.Window2" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WPFdemo12" 7 mc:Ignorable="d" 8 Title="Window2" Height="300" Width="300"> 9 <Window.Resources> 10 11 <Style TargetType="{x:Type Button}"> 12 <Style.Triggers> 13 <!--事件触发器:当 window 加载时,触发--> 14 <EventTrigger RoutedEvent="Window.Loaded"> 15 <EventTrigger.Actions> 16 <!--同样的,启动动画并将动画分发给目标对象和属性--> 17 <BeginStoryboard> 18 <Storyboard> 19 <DoubleAnimation Storyboard.TargetProperty="Width" From="0" To="100" Duration="0:0:5"/> 20 </Storyboard> 21 </BeginStoryboard> 22 </EventTrigger.Actions> 23 </EventTrigger> 24 </Style.Triggers> 25 </Style> 26 </Window.Resources> 27 28 <StackPanel Margin="5"> 29 <Button Padding="20" Margin="5" >666666</Button> 30 31 </StackPanel> 32 </Window>
四,数据触发器
当绑定数据满足某些条件时,数据触发器执行某些操作。
1 <Window x:Class="WPFdemo12.Window3" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WPFdemo12" 7 mc:Ignorable="d" 8 Title="Window3" Height="300" Width="300"> 9 <Window.Resources> 10 11 <Style TargetType="{x:Type Button}"> 12 <Style.Triggers> 13 <!--触发条件:当 name 值为 bt 的控件的焦点属性值为 True 时--> 14 <DataTrigger Binding="{Binding IsFocused, ElementName=bt}" Value="True"> 15 <Setter Property="Background" Value="Blue"></Setter> 16 <DataTrigger.EnterActions> 17 <!--同样的,启动动画并将动画分发给目标对象和属性--> 18 <BeginStoryboard> 19 <Storyboard> 20 <DoubleAnimation Storyboard.TargetProperty="Width" 21 From="100" To="500" Duration="0:0:4"/> 22 </Storyboard> 23 </BeginStoryboard> 24 </DataTrigger.EnterActions> 25 </DataTrigger> 26 </Style.Triggers> 27 </Style> 28 </Window.Resources> 29 <Grid> 30 <Button Name="bt" Content="66666" Margin="30"></Button> 31 </Grid> 32 </Window>
多重数据绑定跟多重绑定类似。