How to stop an animation in C# / WPF?

C#Wpf

C# Problem Overview


I have something like this:

barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation(
barProgress.Value, dNextProgressValue,
new Duration(TimeSpan.FromSeconds(dDuration)));

Now, how would you stop that animation (the DoubleAnimation)? The reason I want to do this, is because I would like to start new animations (this seems to work, but it's hard to tell) and eventually stop the last animation...

C# Solutions


Solution 1 - C#

To stop it, call BeginAnimation again with the second argument set to null.

Solution 2 - C#

When using storyboards to control an animation, make sure you set the second parameter to true in order to set the animation as controllable:

public void Begin(
    FrameworkContentElement containingObject,
    **bool isControllable**
)

Solution 3 - C#

There are two ways to stop a BeginAnimation. The first is to call BeginAnimation again with the second parameter set to null. This will remove all animations on the property and revert the value back to its base value.

Depending on how you are using that value this may not be the behavior you want. The second way is to set the animations BeginTime to null then call BeginAnimation with it. This will remove that specific animation and leave the value at its current position.

DoubleAnimation myAnimation = new Animation();
// Initialize animation
...

// To start
element.BeginAnimation(Property, myAnimation);

// To stop and keep the current value of the animated property
myAnimation.BeginTime = null;
element.BeginAnimation(Property, myAnimation);

Solution 4 - C#

<Trigger.EnterActions>
       <BeginStoryboard x:Name="myStory">
       .........
       </BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
       <StopStoryboard BeginStoryboardName="myStory"/>
</Trigger.ExitActions>

Solution 5 - C#

> If you want the base value to become > the effective value again, you must > stop the animation from influencing > the property. There are three ways to > do this with storyboard animations: > > - Set the animation's FillBehavior > property to Stop > - Remove the entire Storyboard > - Remove the animation from the > individual property

From MSDN

How to: Set a Property After Animating It with a Storyboard

Solution 6 - C#

In my case I had to use two commands, my xaml has a button which fires a trigger, and its trigger fires the storyboard animation.

I've put a button to stop animation with this code behind:

MyBeginStoryboard.Storyboard.Begin(this, true);
MyBeginStoryboard.Storyboard.Stop(this);

I don't like it but it really works here. Give it a try!

Solution 7 - C#

Place the animation in a StoryBoard. Call Begin() and Stop() on the storyboard to start to stop the animations.

Solution 8 - C#

You can use this code:

[StoryBoardName].Remove([StoryBoardOwnerControl]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaren ThomasView Question on Stackoverflow
Solution 1 - C#TheSmurfView Answer on Stackoverflow
Solution 2 - C#user3837View Answer on Stackoverflow
Solution 3 - C#BruceLHView Answer on Stackoverflow
Solution 4 - C#FawazView Answer on Stackoverflow
Solution 5 - C#NickView Answer on Stackoverflow
Solution 6 - C#Junior MayhéView Answer on Stackoverflow
Solution 7 - C#Brian LeahyView Answer on Stackoverflow
Solution 8 - C#oliverView Answer on Stackoverflow