51

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

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200

8 Answers8

90

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

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
TheSmurf
  • 15,337
  • 3
  • 40
  • 48
  • 6
    Be careful. This doesn't work if you specify HandoffBehavior.Compose. See http://msdn.microsoft.com/en-us/library/ms590757%28v=VS.90%29.aspx for details. Pay attention to the doco for the animation parameter. – LukeN Jun 23 '11 at 01:03
  • 3
    Calling `BeginAnimation` a second time doesn't seem to stop the Animation "clock" of the first animation from going on. After the specified time the animation raises the `Completed` event. Is is not raised directly when calling `BeginAnimation` but rather when the Animation *would have finished*. – BatteryBackupUnit Jun 08 '15 at 12:49
  • Please show me how to set second argument to null in vb.net??? `myStoryboard.Begin(containingObject:=Me, isControllable:=True, handoffBehavior:=Animation.HandoffBehavior.SnapshotAndReplace)` – Herry Markowitz Mar 18 '19 at 03:08
  • @HerryMarkowitz in vb.net use the keyword `Nothing`. -- https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing – BrainSlugs83 Jul 06 '20 at 19:45
34

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**
)
bas
  • 13,550
  • 20
  • 69
  • 146
user3837
  • 2,236
  • 2
  • 15
  • 6
18

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);
BruceLH
  • 321
  • 3
  • 7
  • I agree. This answers the question, applying specifically to the OPs request for Animations, not a Storyboard, and provides two good options. – Grim Feb 26 '16 at 12:17
  • Note again, this does not work if you are using `HandoffBehavior.Compose`. – BrainSlugs83 Jul 06 '20 at 19:49
11
<Trigger.EnterActions>
       <BeginStoryboard x:Name="myStory">
       .........
       </BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
       <StopStoryboard BeginStoryboardName="myStory"/>
</Trigger.ExitActions>
Fawaz
  • 3,404
  • 3
  • 17
  • 22
9

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!

Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
  • 3
    Remember to call Storyboard.Remove(this) as well, or you may leak memory since your FrameworkElement will keep a reference to the storyboard. I just found an instance in my code where just stopping and replacing the animation left 4000+ animations rooted after running for some weeks. – LukeN Jun 23 '11 at 01:07
8

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

Nick
  • 2,555
  • 1
  • 20
  • 18
4

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

Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
0

You can use this code:

[StoryBoardName].Remove([StoryBoardOwnerControl]);
oliver
  • 362
  • 2
  • 5
  • 13