【问题标题】:How to reset a color animation to the color it was before the animation with WPF triggers and animations如何使用 WPF 触发器和动画将颜色动画重置为动画之前的颜色
【发布时间】:2013-08-12 08:06:26
【问题描述】:

我有一个文本框的控件模板,其中包含这样的触发器部分

<ControlTemplate.Triggers>
    <EventTrigger  RoutedEvent="Binding.TargetUpdated">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimationUsingKeyFrames 
                    Storyboard.TargetName="Border"
                    Storyboard.TargetProperty="Background.(SolidColorBrush.Color)">
                    <EasingColorKeyFrame KeyTime="0:0:0.20" Value="Yellow"/>
                    <EasingColorKeyFrame KeyTime="0:0:1" Value="{Binding ElementName=Border, Path=Background.SolidColorBrush.Color}"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</ControlTemplate.Triggers>

这个想法是每当绑定目标更新时,文本框都会发出黄色脉冲。我的 UI 在控件之间有复杂的依赖关系,我希望通过简单的视觉提示在事情发生变化时通知用户。

我上面的问题是将文本框的背景颜色重置为以前的颜色。如果我将它动画化回白色,这可能不是原始颜色。有几种视觉状态,即normaldisabledenabled

所以我希望脉冲黄色,然后恢复到以前的颜色。但是,如果我尝试绑定最终关键帧的颜色,则会收到类似的错误

Cannot freeze storyboard to be used across multiple threads.

有没有办法在动画完成后自动清除动画结果或动态绑定正确的颜色?

【问题讨论】:

  • 您不能使用Storyboard 中的表达式绑定。请参阅here(Animate in Style)。相反,例如使用附加的依赖属性,它将存储 Background 的先前值。

标签: wpf triggers controltemplate


【解决方案1】:

您可以将动画的FillBehavior 设置为Stop。然后,动画属性将自动恢复为动画开始之前的值。

<ColorAnimationUsingKeyFrames FillBehavior="Stop"
    Storyboard.TargetName="Border"
    Storyboard.TargetProperty="Background.Color">
    ...
</ColorAnimationUsingKeyFrames>

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2014-11-13
    • 1970-01-01
    • 2012-12-22
    • 2014-10-20
    • 2015-05-18
    相关资源
    最近更新 更多