Wpf – Restarting a WPF Storyboard


What is the proper way to stop and restart a storyboard from .net code?

I'm trying …


Expecting that a subsequent call to .Begin(this); would restart from the timeline at zero, but instead, the storyboard picks up right where it was stopped.

I have tried


and I tried


which also didn't work.

More details … Here is my storyboard sample.

<Storyboard x:Key="overlay">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textone" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="0"/>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="texttwo" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="0"/>

So the text in the textone runs, and if you close the screen, and return to the screen quickly, the texttwo, actually plays over a newly started storyboard. So the original (from the first screen) storyboard is still around and playing, even though I've removed, and stopped it.

Best Solution

What about using Storyboard.Seek(TimeSpan.Zero)? Similar to seeking in a Stream, this should bring you back to the beginning of the animation.

I commented that you should also make sure that the IsControllable property is set to true. Keep that in mind!

Storyboard.Seek method