一、属性触发器
要改变的属性值不能再控件里面设置初始值,否则不能触发,如下例的Width
<Rectangle Height="100" Fill="Black" Name="R1" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform></RotateTransform>
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Width" Value="100"></Setter>
<Style.Triggers>
<Trigger Property="Fill" Value="Black">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimation Storyboard.TargetProperty="Height" From="100" To="200" Duration="0:0:1"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Setter Property="Width" Value="200"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
二、数据触发器
相比对属性触发器而言,他可以绑定其他控件的属性或者ViewModel里面的属性
<Rectangle Width="200" Name="R2" Fill="Green" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform >
<RotateTransform x:Name="R2RT" Angle="0"></RotateTransform>
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Height" Value="100"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=R1,Path=Fill}" Value="Black">
<Setter Property="Height" Value="200"></Setter>
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever" >
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
三、事件触发器
- 只能触发故事板(不知道是否正确)
- 分为控件触发器和样式触发器(这两个名字自己取的,也不知道有没有官方说法)
控件触发器( Storyboard.TargetName似乎专为它服务)
<Rectangle Width="200" Height="200" Name="R3" RenderTransformOrigin="0.5,0.5" Fill="Yellow">
<Rectangle.RenderTransform>
<RotateTransform x:Name="R3RT"></RotateTransform>
</Rectangle.RenderTransform>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="R2RT" Storyboard.TargetProperty="Angle" From="0" To="360" RepeatBehavior="Forever" Duration="0:0:1"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="R3RT" Storyboard.TargetProperty="Angle" From="0" To="360" RepeatBehavior="Forever" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
样式触发器
<Rectangle Width="200" Height="200" Name="R4" RenderTransformOrigin="0.5,0.5" Fill="Yellow">
<Rectangle.RenderTransform>
<RotateTransform x:Name="R4RT"></RotateTransform>
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)" From="0" To="360" RepeatBehavior="Forever" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>