title: Changing rectangle margin with code
body: <p>I want to move my Rectangle by pressing down keys, but currently I am getting error that says:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.ArgumentException' occurred in
    WindowsBase.dll</p>
    
    <p>Additional information: 'Auto,Auto,0,0' is not a valid value for
    property 'Margin'.</p>
  </blockquote>
  
  <p>My code is following:</p>
  
  <pre><code>private void Window_KeyDown(object sender, KeyEventArgs e)
          {
              double x = Canvas.GetLeft(rect);
              double y = Canvas.GetTop(rect);
  
              if (e.Key == Key.D)
              {
                  rect.Margin = new Thickness(x+5, y, 0, 0);
              }
              else if (e.Key == Key.A)
              {
                  rect.Margin = new Thickness(x-5, y, 0, 0);
              }
              Thread.Sleep(100);
          }
  </code></pre>