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(),
    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);}

Categories: .NET, All, WPF

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.