【问题标题】:WPF stop and reverse Storyboard animation when new animation starts新动画开始时 WPF 停止和反转情节提要动画
【发布时间】:2015-06-04 12:16:33
【问题描述】:

我有 2 个 DataTrigger 可以为按钮的背景颜色设置动画(反向),这部分工作正常。

问题在于,当两个触发器在重叠时间内触发时,第一个触发的触发器不会反转颜色,当第二个触发器停止时,它会将颜色反转为前一个动画停止时的颜色。

我该怎么办?

这是示例代码:

<DataTrigger Binding="{Binding IsUp}" Value="True">
    <DataTrigger.EnterActions>
        <StopStoryboard BeginStoryboardName="isDownStoryBoard"/>
            <BeginStoryboard Name="isUpStoryBoard">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[0].Color" To="#b4e391" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[1].Color" To="#61c419" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[2].Color" To="#b4e391" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[3].Color" To="#b4e391" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />                           
            </Storyboard>                                                                                                                                     
        </BeginStoryboard>                                                                                                                                    
    </DataTrigger.EnterActions>                                                                                                                                                                                   
</DataTrigger>                                                                                                                                                

<DataTrigger Binding="{Binding IsDown}" Value="True">                                                                                                         
    <DataTrigger.EnterActions>
        <StopStoryboard BeginStoryboardName="isUpStoryBoard"/>
            <BeginStoryboard Name="isDownStoryBoard">                                                                                                                                     
                <Storyboard>                                                                                                                                      
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[0].Color" To="#efc5ca" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[1].Color" To="#d24b5a" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[2].Color" To="#ba2737" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
                    <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[3].Color" To="#f18e99" Duration="0:0:1" AutoReverse="True" RepeatBehavior="2x" />
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>     
</DataTrigger>   

【问题讨论】:

    标签: c# wpf datatrigger wpf-animation coloranimation


    【解决方案1】:

    有一个StopStoryboard Class 可以用来停止正在运行的Storyboard,但没有ReverseStoryboard 类。但是,您可以创建另一个 Storyboard,它与您在停止原始的时启动的原始的相反。您可以像这样使用StopStoryboard 类(来自 MSDN 上的链接页面):

    <!-- Begin the Storyboard -->
    <EventTrigger RoutedEvent="Button.Click" SourceName="BeginButton">
      <BeginStoryboard Name="MyBeginStoryboard">
        <Storyboard >
          <DoubleAnimation 
            Storyboard.TargetName="myRectangle" 
            Storyboard.TargetProperty="Width" 
            Duration="0:0:5" From="100" To="500" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
    
    ...
    
    <!-- Stop the Storyboard -->
    <EventTrigger RoutedEvent="Button.Click" SourceName="StopButton">
      <StopStoryboard BeginStoryboardName="MyBeginStoryboard" />
    </EventTrigger>
    

    另外,请查看您可以在Storyboard 上设置的Timeline.FillBehavior Property,该Storyboard 获取或设置一个值,该值指定时间线在其活动期结束后的行为方式。您可以将其设置为 FillBehavior.HoldEnd 枚举以获取 Storyboard 以在结束时保留其最后一个值。

    【讨论】:

    • 谢谢谢里登。这很好,但我不知道之前的状态是什么,这就是为什么我希望它反转到动画开始时的状态
    • 抱歉,没有ReverseStoryboard 类。您可以随时尝试以编程方式访问 Storyboard 并更改其属性,但我不建议这样做。
    猜你喜欢
    • 2011-08-03
    • 2014-04-16
    • 2013-07-30
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多