title: How to create BeginStoryboard in code behind for WPF?
body: <p>I have the following XAML and I wish to convert this to code behind, I have been able to successfully create the animations so the control fades in &amp; out as expected, but I'm having trouble converting the IsMouseOver trigger to code behind:</p>
  
  <pre><code> &lt;DataTemplate.Triggers&gt;
              &lt;EventTrigger RoutedEvent="Control.Loaded"
                            SourceName="NotificationGrid"&gt;
                  &lt;BeginStoryboard x:Name="BeginNotificationStoryboard"&gt;
                      &lt;Storyboard x:Name="NotificationStoryboard"&gt;
                          &lt;DoubleAnimation Storyboard.TargetName="NotificationGrid"
                                           From="0.01"
                                           To="1"
                                           Storyboard.TargetProperty="Opacity"
                                           Duration="0:0:0.5" /&gt;
  
                          &lt;DoubleAnimation Storyboard.TargetName="NotificationGrid"
                                           From="1"
                                           To="0"
                                           Storyboard.TargetProperty="Opacity"
                                           Duration="0:0:0.5"
                                           BeginTime="0:0:5" /&gt;
                      &lt;/Storyboard&gt;
                  &lt;/BeginStoryboard&gt;
              &lt;/EventTrigger&gt;
              &lt;Trigger Property="IsMouseOver"
                       Value="True"&gt;
                  &lt;Trigger.EnterActions&gt;
                      &lt;SeekStoryboard Offset="0:0:3"
                                      BeginStoryboardName="BeginNotificationStoryboard" /&gt;
                      &lt;PauseStoryboard BeginStoryboardName="BeginNotificationStoryboard" /&gt;
                  &lt;/Trigger.EnterActions&gt;
                  &lt;Trigger.ExitActions&gt;
                      &lt;SeekStoryboard Offset="0:0:3"
                                      BeginStoryboardName="BeginNotificationStoryboard" /&gt;
                      &lt;ResumeStoryboard BeginStoryboardName="BeginNotificationStoryboard" /&gt;
                  &lt;/Trigger.ExitActions&gt;
              &lt;/Trigger&gt;
          &lt;/DataTemplate.Triggers&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The issue I am have is how do I know what the 'BeginStoryboardName' value should be for the instances of the SeekStoryboard classes since I haven't had to create a BeginStoryboard instance to get the animation on loading to work as expected.</p>
  
  <pre><code>var loadingAnimation = new DoubleAnimation(0.01, 1, new Duration(TimeSpan.FromSeconds(0.5)));
  var closingAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(3)))
  {
      BeginTime = TimeSpan.FromSeconds(5)
  };
  
  Storyboard.SetTarget(loadingAnimation, AssociatedObject);
  Storyboard.SetTarget(closingAnimation, AssociatedObject);
  
  Storyboard.SetTargetProperty(loadingAnimation, new PropertyPath(UIElement.OpacityProperty));
  Storyboard.SetTargetProperty(closingAnimation, new PropertyPath(UIElement.OpacityProperty));
  
  Storyboard.SetTarget(loadingAnimation, AssociatedObject);
  Storyboard.SetTarget(closingAnimation, AssociatedObject);
  
  var storyboard = new Storyboard();
  storyboard.Children.Add(loadingAnimation);
  storyboard.Children.Add(closingAnimation);
  
  var enterSeekStoryboard = new SeekStoryboard
  {
      Offset = TimeSpan.FromSeconds(5),
      // What value should go here?
      BeginStoryboardName = ""
  };
  
  var exitSeekStoryboard = new SeekStoryboard
  {
      Offset = TimeSpan.FromSeconds(5),
      // What value should go here?
      BeginStoryboardName = ""
  };
  
  var trigger = new Trigger
  {
      Property = UIElement.IsMouseOverProperty,
      Value = true
  };
  
  trigger.EnterActions.Add(enterSeekStoryboard);
  trigger.ExitActions.Add(exitSeekStoryboard);
  
  var style = new Style();
  style.Triggers.Add(trigger);
  
  AssociatedObject.Style = style;
  
  storyboard.Completed += HandleOnCompleted;
  storyboard.Begin();
  </code></pre>