How to reset a color animation to the color it was before the animation with WPF triggers and animations

StackOverflow https://stackoverflow.com/questions/18182126

  •  24-06-2022
  •  | 
  •  

Domanda

I havea control template for a text box with a triggers section like this

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

The idea being that whenever the binding target is updated the text box will pulse yellow. My UI has complex dependencies between controls and i would like the user to be notified when things change via a simple visual cue.

The problem I have above is resetting the color of the background of textbox to the color it was previously. If I animate it back to white this might not have been the original color. There are several visual states, ie normal disabled enabled.

So I wish to pulse yellow and then return to color it was previously. However if I try to bind the color of the final keyframe I get an error like

Cannot freeze storyboard to be used across multiple threads.

Is there either a way to clear the result of the animation automatically after it finishes or bind the correct color in dynamically?

È stato utile?

Soluzione

You could set the animation's FillBehavior to Stop. The animated property will then automatically revert to the value it had before the animation was started.

<ColorAnimationUsingKeyFrames FillBehavior="Stop"
    Storyboard.TargetName="Border"
    Storyboard.TargetProperty="Background.Color">
    ...
</ColorAnimationUsingKeyFrames>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top