新动画开始时WPF停止和反转Storyboard动画

时间:2015-06-04 12:16:33

标签: c# wpf datatrigger wpf-animation coloranimation

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

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

我该怎么办?

这是示例代码:

<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>   

1 个答案:

答案 0 :(得分: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 获取或设置一个值,该值指定时间轴在达到活动期间结束后的行为方式。< / em>您可以将其设置为FillBehavior.HoldEnd枚举,以使Storyboard在结束时保留最后一个值。