当属性值发生变化时,此示例演示如何使用Trigger来启动Storyboard。 可以在Style、ControlTemplate或DataTemplate中使用Trigger。
示例:
以下示例使用一个Trigger来动画化Opacity的Button,当它的IsMouseOver属性变为true
时。
<!-- PropertyTriggerExample.xaml
Shows how to use property triggers to start animations. -->
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Animate Properties with Storyboards">
<Page.Resources>
<Style x:Key="PropertyTriggerExampleButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Opacity" Value="0.25" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="0.25" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel Margin="20">
<Button Style="{StaticResource PropertyTriggerExampleButtonStyle}">
Move the mouse over me.
</Button>
</StackPanel>
</Page>
属性Trigger对象应用的动画的行为比EventTrigger动画或使用Storyboard方法开始的动画更为复杂。 它们使用由其他 Trigger 对象定义的动画进行“切换”,但使用 EventTrigger 和方法触发的动画进行组合。