title: How to stop WPF storyboard composed only in XAML
body: <p>I have a animation defined in XAML (as a UserControl) that essentially switches between two images every second: </p>
  
  <pre><code>    &lt;UserControl x:Class="KaleidoscopeApplication.Controls.RemoteAnimation"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Loaded="RemoteAnimation_Loaded"
      Unloaded="RemoteAnimation_Unloaded"&gt;
  
      &lt;Grid Canvas.Left="500" Canvas.Top="84"&gt;
          &lt;Grid.Triggers&gt;
              &lt;EventTrigger RoutedEvent="Grid.Loaded"&gt;
                  &lt;BeginStoryboard&gt;
                      &lt;Storyboard x:Name="storyboard"  RepeatBehavior="Forever"&gt;
                          &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="remote2" BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)"&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0:0:1"&gt;
                                  &lt;DiscreteObjectKeyFrame.Value&gt;
                                      &lt;Visibility&gt;Collapsed&lt;/Visibility&gt;
                                  &lt;/DiscreteObjectKeyFrame.Value&gt;
                              &lt;/DiscreteObjectKeyFrame&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0:0:2"&gt;
                                  &lt;DiscreteObjectKeyFrame.Value&gt;
                                      &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                  &lt;/DiscreteObjectKeyFrame.Value&gt;
                              &lt;/DiscreteObjectKeyFrame&gt;
                          &lt;/ObjectAnimationUsingKeyFrames&gt;
                      &lt;/Storyboard&gt;
                  &lt;/BeginStoryboard&gt;
              &lt;/EventTrigger&gt;
          &lt;/Grid.Triggers&gt;
  
          &lt;Image Name="remote1" Source="/Resources/Elements/Images/341.png"/&gt;
          &lt;Image Name="remote2" Source="/Resources/Elements/Images/342.png"/&gt;
  
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>It can be used in a window thusly:</p>
  
  <pre><code>&lt;!-- Remote --&gt;
  &lt;uControl:RemoteAnimation 
       x:Name="remoteAnimation"
       Canvas.Left="316" Canvas.Top="156" Height="246" Width="121" /&gt;
  </code></pre>
  
  <p>My problem is that when the window containing the animation closes, it keeps on running and causes a leak. I'm not able to stop the animation via RemoteAnimation_Unloaded() with storyboard.Stop()... it doesn't do jack.</p>
  
  <p>I've checked out both of those posts, but they don't apply:</p>
  
  <p><a href="https://stackoverflow.com/questions/1499477/wpf-window-close-not-triggering-usercontrol-unloaded-event">Post1</a>
  <a href="https://stackoverflow.com/questions/502761/disposing-wpf-user-controls">Post2</a></p>
  
  <p>I am able to get into the unloaded method, but calling Stop() does not stop the animation. From my understanding, it may be an issue with a call to Begin() for the storyboard. There is an overload with an isControlable parameter. However, since the animation is completely in XAML, I'm not sure how to affect this. </p>