How to stop an animation in WPF

2k views Asked by At

I have the following code to start an animation that it's located in my Resources:

 Storyboard _wheelAnimation = FindResource("CircleRotation") as Storyboard;
 if (_wheelAnimation == null) return;
 _wheelAnimation.Begin(this);

Here is my animation:

<Storyboard x:Key="CircleRotation" RepeatBehavior="Forever">
    <DoubleAnimation 
        Storyboard.TargetName="BigWheelImage"
        Storyboard.TargetProperty="RenderTransform.Angle"
        From="360" To="0" Duration="0:0:35" BeginTime="00:00:00.000" />
    <DoubleAnimation 
        Storyboard.TargetName="SmallWheelImage"
        Storyboard.TargetProperty="RenderTransform.Angle"
        From="0" To="360" Duration="0:0:25" BeginTime="00:00:00.000" />
</Storyboard>

I want to stop it programmatically, and I tried this:

_wheelAnimation.Stop();  // and also _wheelAnimation.Stop(this);

With no luck... any idea on why it's not stopping?

2

There are 2 answers

0
Bahman_Aries On BEST ANSWER

When using Begin, Set the second parameter (IsControllable) to true in order to make the Stop method work:

_wheelAnimation.Begin(this, true);
_wheelAnimation.Stop(this);
2
Ben Cohen On

Try _wheelAnimation.SkipToFill();