zoukankan      html  css  js  c++  java
  • 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>
    View Code

       详细代码: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>
    View Code

    **特别注意:

             由于EventTrigger本身就是路由事件触发器,路由事件可以通过VS查阅API获得,

          查询的方式,如:

        Button通过F12看到按钮的公开代码,Button自身没有RoutedEvent,然后查询其父类ButtonBase,有一个ClickEvent;

               再查询ButtonBase的父类ContentControl...一直到UIElement类,你就会发现很多RoutedEvent。

           EnterActions和ExitActions不能用于EventTrigger,否则会报错Event triggers do not support EnterActions or ExitActions.

  • 相关阅读:
    C语言面向对象编程(五):单链表实现(转)
    pthread_barrier_init,pthread_barrier_wait简介(转)
    Linux信号(signal) 机制分析(转)
    Linux 进程间通信 --- 信号通信 --- signal --- signal(SIGINT, my_func); --- 按键驱动异步通知(转)
    Linux 多线程环境下 进程线程终止函数小结(转)
    批量杀进程 ps awk grep
    C++中的INL(转)
    gdb调试多进程和多线程命令(转)
    转: 基于netty+ protobuf +spring + hibernate + jgroups开发的游戏服务端
    转:全栈工程师的知识栈列表
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6496684.html
Copyright © 2011-2022 走看看