title: Add Dependency Property to existing .NET class
body: <p>IN a WPF project I have a bunch of controls in which I would like to be able to set individual <code>Margin</code> properties and conserve the other values. So, I would like to avoid setting the complete margin to a new <code>Thickness</code> (<code>Margin="0,5,0,15"</code>). Because many margin's are set from styles etc. But in individual cases I would like to deviate from the generic styles for certain controls.</p>
  
  <p>I thought, why not register a couple of new dependency properties on the .NET class <code>FrameWorkElement</code> like so (for example only MarginLeft is shown):</p>
  
  <pre><code>public class FrameWorkElementExtensions: FrameworkElement
  {
      public static readonly DependencyProperty MarginLeftProperty = DependencyProperty.Register("MarginLeft", typeof(Int16?), typeof(FrameworkElement), new PropertyMetadata(null, OnMarginLeftPropertyChanged));
      public Int16? MarginLeft
      {
          get { return (Int16?)GetValue(MarginLeftProperty); }
          set { SetValue(MarginLeftProperty, value); }
      }
  
      private static void OnMarginLeftPropertyChanged(object obj, DependencyPropertyChangedEventArgs e)
      {
          if (obj != null &amp;&amp; obj is UIElement)
          {
              FrameworkElement element = (FrameworkElement)obj;
  
              element.Margin = new Thickness((Int16?)e.NewValue ?? 0, element.Margin.Top, element.Margin.Right, element.Margin.Bottom);
          }
      }
  }
  </code></pre>
  
  <p>But this property doesn't come available in code-behind or in XAML. I can understand it somehow, because this dummy class is never instantiated or whatsoever. Tried to make it a static class but then you can't derive from FrameWorkElement (which I need for the GetValue and SetValue methods).</p>
  
  <p>I couldn't find any resource on the net that treats the more generic question: Can you add dependency properties to exiting .NET classes?</p>
  
  <p>Any help / wise advice is appreciated.</p>
  
  <p>BTW: a solution for changing only one component of a Margin (Thickness) is also appreciated ;) </p>