title: Creating Storyboard with DoubleAnimationUsingKeyFrames in Code Behind C#
body: <p>This is my XAML that works perfectly : </p>
  
  <pre><code>&lt;Window
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
          xmlns:local="clr-namespace:WpfApplication1" 
          x:Class="WpfApplication1.MainWindow"
          x:Name="Window"
          xmlns:converter="clr-namespace:WpfApplication1.Image"
          Title="MainWindow" Height="350" Width="525"
          Loaded="MainWindow_OnLoaded"&gt;
      &lt;Window.Resources&gt;
          &lt;Storyboard x:Key="Move" RepeatBehavior="Forever"&gt;
              &lt;DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(local:FloatingTextbox.Position)" Storyboard.TargetName="FloatingTextbox"&gt;
                  &lt;EasingDoubleKeyFrame KeyTime="0" /&gt;
                  &lt;EasingDoubleKeyFrame KeyTime="0:0:6" Value="300"/&gt;
              &lt;/DoubleAnimationUsingKeyFrames&gt;
          &lt;/Storyboard&gt;
          &lt;converter:InitialPositionConverter x:Key="InitialPositionConverter"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid Background="Black"&gt;
          &lt;Canvas Height="100" Width="411.111" Margin="45.833,80.556,0,0" Background="White"&gt;
              &lt;local:FloatingTextbox x:Name="FloatingTextbox" Height="37.222" 
                                     TextWrapping="Wrap" Text="FloatingTextbox" Width="100" 
                                     BorderThickness="1" Position="1" Canvas.Top="26.389"&gt;
                  &lt;Canvas.Left&gt;
                      &lt;MultiBinding Converter="{StaticResource InitialPositionConverter}" Mode="OneWay"&gt;
                          &lt;Binding Path="Position" RelativeSource="{RelativeSource Self}"/&gt;
                          &lt;Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/&gt;
                      &lt;/MultiBinding&gt;
                  &lt;/Canvas.Left&gt;
                  &lt;i:Interaction.Triggers&gt;
                      &lt;i:EventTrigger EventName="Loaded"&gt;
                          &lt;ei:ControlStoryboardAction Storyboard="{StaticResource Move}"/&gt;
                      &lt;/i:EventTrigger&gt;
                  &lt;/i:Interaction.Triggers&gt;
              &lt;/local:FloatingTextbox&gt;
          &lt;/Canvas&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>I want to create the storyboard and realize the animation in Code, I tried this:</p>
  
  <pre><code>private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
          {
              var storyboard = new Storyboard();
              storyboard.RepeatBehavior = new RepeatBehavior(4);
  
              var myDoubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
  
              var myEasingDoubleKeyFrame1 = new EasingDoubleKeyFrame();
              myEasingDoubleKeyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
  
              var myEasingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
              myEasingDoubleKeyFrame2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6));
              myEasingDoubleKeyFrame2.Value = this.ActualWidth;
  
              myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame1);
              myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame2);
  
              Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath(FloatingTextbox.PositionProperty));
              Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, FloatingTextbox.Name);
  
              storyboard.Children.Add(myDoubleAnimationUsingKeyFrames);
  
              var eventTrigger = new System.Windows.Interactivity.EventTrigger() { EventName = "Loaded" };
  
              var triggers = Interaction.GetTriggers(FloatingTextbox);
              triggers.Add(eventTrigger);
  
              var controlStoryboardAction = new ControlStoryboardAction();
              controlStoryboardAction.Storyboard = storyboard;
              controlStoryboardAction.ControlStoryboardOption = ControlStoryboardOption.Play;
              eventTrigger.Actions.Add(controlStoryboardAction);
          }
  </code></pre>
  
  <p><strong>Note :</strong> The reason I want to realize this in code behind, is to put the window's <code>ActualWidth</code> instead of a <code>300</code> here :</p>
  
  <pre><code>&lt;EasingDoubleKeyFrame KeyTime="0:0:6" Value="300"/&gt;
  </code></pre>
  
  <p>I can't bind since, it's freezable, so ... that's why.</p>
  
  <p>My C# Code doesn't work, what should I be missing  ??</p>