title: WPF. Easiest way to move Image to (X,Y) programmatically?
body: <p>Does anyone know of an easy way to animate a movement from an Image's current location to a new location (X,Y) using WPF animation with no XAML, 100% programmatically? And with no reference to "this" (with RegisterName etc).</p>
  
  <p>I am trying to make an extension class for Image to do animation stuff on it. It is easy enough to change the width and height properties through animation, but after searching for location animation of an object it suddenly becomes more advanced.</p>
  
  <p>As it is an extension class I will only have a reference to the actual Image object and the X and Y I want to move it to.</p>
  
  <pre><code>public static void MoveTo(this Image targetControl, double X, double Y, double Width, double Height){
   //code here
   ...
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>Thanks. Almost working. It seems The GetTop and GetLeft returns 'NaN' not explicitly set. Found the workaround in this post: <a href="https://stackoverflow.com/questions/669071/canvas-gettop-returning-nan">Canvas.GetTop() returning NaN</a></p>
  
  <pre><code>public static void MoveTo(this Image target, double newX, double newY) {
                  Vector offset = VisualTreeHelper.GetOffset(target);
                  var top = offset.Y;
                  var left = offset.X;
                  TranslateTransform trans = new TranslateTransform();
                  target.RenderTransform = trans;
                  DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromSeconds(10));
                  DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromSeconds(10));
                  trans.BeginAnimation(TranslateTransform.YProperty, anim1);
                  trans.BeginAnimation(TranslateTransform.XProperty, anim2);
          }
  </code></pre>
  
  <p>I had to swap two of the values (FROM) with 0. I assume that must be because in this context the upper left corner of the picture is the origin? But now it works.</p>