title: How can i replay the same animation every time button pressed " WPF Animmation"
body: <p>How can i replay the same animation every time button pressed  1st time i press btn animation  play but nxt there is runtime error </p>
  
  <pre><code>    private void Button_Click(object sender, RoutedEventArgs e)
      {
          int j = 0;
          string[] names ={"/Assets/1.png", "/Assets/2.png", "/Assets/3.png", "/Assets/4.png", "/Assets/5.png" };
  
          var storyboard = new Storyboard
          {
  
  
          };
  
          var animation = new ObjectAnimationUsingKeyFrames();
  
          Storyboard.SetTarget(animation,img);
          Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
  
          storyboard.Children.Add(animation);
  
          for (int i=0 ; i &lt;=4; i++)
          {
             // j = j + 1;
              var keyframe = new DiscreteObjectKeyFrame
              {
                  KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300* i)),
  
  
                 Value = String.Format(names[i])
  
              };
  
              animation.KeyFrames.Add(keyframe);
  
          }
  
              Resources.Add("Storyboard", storyboard);
              // Resources.Add("Storyboard", storyboard);
  
              storyboard.Begin();
             // storyboard.Completed += new EventHandler(Story_Completed);
  
  
             // Thread.Sleep(1000);
              if (j==4)
              {
                  storyboard.Pause();
  
          }
  
          }
  </code></pre>