Dependency Property

Dependency properties are properties backed by WPF engine. They use more sufficient storage and support for additional feature such as change notifications & property value inheritance.

These are also the basis for a number of key WPF features like animation, data binding and styles.

Dependency properties in the WPF library are always wrapped by ordinary .Net property procedures.

e.g. Margin for any container control like StackPanel etc.

Steps in Creation of Own Dependency Property : 

  • Define an object that represents your property. (Add Property keyword at the end of property name)
public static readonly DependencyProperty MarginProperty;
  • Register a dependency property
static FrameworkElement()
     FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(new Thickness(),
                                                                                       FrameworkPropertyMetadataOptions.AffectsMeasure);MarginProperty = DependencyProperty.Register("Margin",typeof(Thickness),
                             typeof(FrameworkElement),metadata,new ValidateValueCallback(FrameworkElement.IsMarginValid));

Adding a Property Wrapper

public Thickness Margin
      set { SetValue(MarginProperty,value);}
     get { return (Thickness)GetValue(MarginProperty);}