WPF中触发器(Trigger、DataTrigger)使用动画最简单的方式EnterActions和ExitsActions
1.当鼠标移入后执行某个动画:
<Style TargetType="{x:Type StackPanel}"> <Setter Property="RenderTransform"> <Setter.Value> <TranslateTransform X="{Binding ElementName=borderContent,Path=ActualWidth}"/> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation To="0" Duration="0:0:0.2" AccelerationRatio="0.5" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.2" AccelerationRatio="0.5" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"/> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> </Style.Triggers> </Style>
详细代码:http://www.cnblogs.com/tommy-huang/p/5714863.html
2.当绑定的属性等于某个数值时的动画
<DataTrigger Binding="{Binding Tag,RelativeSource={RelativeSource Self},Converter={StaticResource DictionaryConverter},ConverterParameter=Type}" Value="Scan"> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard RepeatBehavior="Forever" x:Name="leftRightSB"> <DoubleAnimation Storyboard.TargetName="tt" Storyboard.TargetProperty="(TranslateTransform.X)" From="0" To="10" Duration="0:0:0.3" BeginTime="0:0:0.0"/> <DoubleAnimation Storyboard.TargetName="tt" Storyboard.TargetProperty="(TranslateTransform.X)" From="10" To="0" Duration="0:0:0.3" BeginTime="0:0:0.3"/> <DoubleAnimation Storyboard.TargetName="tt" Storyboard.TargetProperty="(TranslateTransform.X)" From="0" To="-10" Duration="0:0:0.3" BeginTime="0:0:0.6"/> <DoubleAnimation Storyboard.TargetName="tt" Storyboard.TargetProperty="(TranslateTransform.X)" From="-10" To="0" Duration="0:0:0.3" BeginTime="0:0:0.9 "/> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> <DataTrigger.ExitActions> <BeginStoryboard> <Storyboard RepeatBehavior="Forever"> <DoubleAnimation Storyboard.TargetName="tt" Storyboard.TargetProperty="(TranslateTransform.Y)" To="0" Duration="0:0:0.01"/> </Storyboard> </BeginStoryboard> </DataTrigger.ExitActions> </DataTrigger>
**特别注意:
由于EventTrigger本身就是路由事件触发器,路由事件可以通过VS查阅API获得,
查询的方式,如:
Button通过F12看到按钮的公开代码,Button自身没有RoutedEvent,然后查询其父类ButtonBase,有一个ClickEvent;
再查询ButtonBase的父类ContentControl...一直到UIElement类,你就会发现很多RoutedEvent。
EnterActions和ExitActions不能用于EventTrigger,否则会报错Event triggers do not support EnterActions or ExitActions.