Reusing a Storyboard

Posted by Corey on March 14, 2008 at 2:46 am.

It is possible to reuse a storyboard.  Make sure to stop the storyboard before you set a new target property.  Thanks Tom for pointing this out.

XAML

    <Canvas x:Name=”LayoutRoot” Background=”White”>
<Button Height=”20″ Width=”120″ Canvas.Left=”0″ Canvas.Top=”0″ Content=”Button” x:Name=”btnTest”/>
<Rectangle x:Name=”Fred” Height=”30″ Width=”30″ Fill=”Aquamarine” Canvas.Top=”50″/>
<Rectangle x:Name=”Wilma” Height=”30″ Width=”30″ Fill=”OrangeRed” Canvas.Top=”150″/>
</Canvas>
C#

        void Page_Loaded(object sender, RoutedEventArgs e)
{
_storyboard = new Storyboard();
_dx = new DoubleAnimation();
_dx.From = 0;
_dx.To = 250;
_dx.Duration = new Duration(new TimeSpan(0,0,3));
_dx.AutoReverse = true;
_dx.RepeatBehavior = RepeatBehavior.Forever;

            Storyboard.SetTargetName(_dx, “Fred”);
Storyboard.SetTargetProperty(_dx, “(Rectangle.Width)”);
_storyboard.Children.Add(_dx);
LayoutRoot.Resources.Add(_storyboard);
_storyboard.Begin();
}

 

        void btnTest_Click(object sender, RoutedEventArgs e)
{
            _storyboard.Stop();
Storyboard.SetTargetName(_dx, “Wilma”);
_storyboard.Begin();
}

Related posts:

  1. Animate multiple transforms from C# – WPF
  2. How to dynamically create animations from C# in Silverlight
  3. Resize Silverlight 1.0 app to size of browser
  4. Animating multiple transforms in C# – Silverlight

2 Responses to “Reusing a Storyboard”

  • 1
    Záboj Jan Says:

    hi…please i need help…i have animation in silverlight with one storyboadr…and i need after push button set a new value of time and resume storyboard…can you halp me please:-)…thank you..;-)

  • 2
    Záboj Jan Says:

    i have got it…thanks..:-)