tags:

views:

719

answers:

1

I have the following storyboard:

<Window.Resources>
 <Storyboard x:Key="ButtonsAnim">
  <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="topRightButton" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
   <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
   <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="-100"/>
  </DoubleAnimationUsingKeyFrames>
  <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="topRightButton" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
   <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
   <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="100"/>
...

It basically moves some buttons around in a canvas.

This is the code that starts the animation:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Storyboard sb = (Storyboard)Resources["ButtonsAnim"];
    storyBoard = sb;
    storyBoard.Begin(this, true);

}

What I am trying to do is to reset the animation when I click a button which hides the window. When the window reappears the animation should start from the beginning.

I tried using storyBoard.Begin(this, true) when the application reappears but for this first milliseconds the buttons are at their last position.

I then tried storyBoard.seek(TimeSpan.Zero) before hiding the window but it fails:

System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Seek'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='24901833'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='24901833'; TargetElement.Type='System.Windows.Media.Animation.Storyboard'

I also tried storyBoard.remove(this) before hiding the window, same effect: the buttons are at their last position.

Any ideas?

Thank you.

A: 

Hi Pek,

I think Storyboard.Stop() should work here. But if you not find anything elegant, you can try reset buttons' transform after you've hided the window. E.g.:

((TranslateTransform)((TransformGroup)topRightButton.RenderTransform)[3]).X = 0;
((TranslateTransform)((TransformGroup)topRightButton.RenderTransform)[3]).Y = 0;

Hope I didn't make any mistake while casting.

NB: You may also find useful this example from MSDN: How to: Control a Storyboard After It Starts

Anvaka