Vasu Balakrishnan’s Blog

WPF Dependency Properties

with one comment

One of the fundamental feature of the WPF framework is the Dependency Properties (DP). When I started to work with WPF, I didn’t fully understand how it all worked. Then as I start to read more about it, I began to understand and appreciate its role. Here I am trying to put together in a nutshell, what I learnt about DP.

  • MSDN Definition – Any property that is backed by WPF property system is a DP. OK thats a vague definition.
  • These are used when the resolution of a property value is based on other properties / runtime settings. Thats the key right here.
  • The resolution process has an order of precedence it assigns to various environmental contexts
  • The rules of precedence are
  1. Property System Coercion
  2. Animations
  3. Local Value
  4. TemplatedParent Template
  5. Style Triggers
  6. Template Triggers
  7. Style Setters
  8. Theme Style
  9. Inheritance
  10. Default Value from DP Metadata
  • DP supports self contained validation – has custom callback methods execute when the value has changed. These can validate the new value or coerce the value into something acceptable
  • Support for Default Values – Has a consistent mechanism to associate default value with the property and has the lowest precedence in the value resolution process
  • Property Metadata – This determines the DP behavior. It is supplied when the property is registered with the system.
  • Value Inheritance – DPs have the ability to inherit its value from the property setting on the ancestor element in the logical tree
  • DPs are the foundation blocks for the data binding in WPF. The DataContext property in FrameworkElement is a defined as a DP. This means all the descendant elements in the logical tree share the same data source.
  • DPs are registered via a static method. You can retrieve value using GetValue and set value using SetValue method
  • To use DP, you’ve to inherit from DependencyObject

AttachedProperty (AP) – This is a special form of Dependency property that is worth mentioning here.

  • One of the purpose of AP, is to let different child elements specify values for property that is defined in a parent element.
  • This works like javascript’s expando properties
  • One example is the DockPanel.Dock property. This is created as an attached property because it is designed to be set on elements that are contained within the DockPanel
  • AP lets child elements inform parent elements how they are to be presented in the UI
  • They are XAML concept, whereas DP as WPF Concept.

Source:

DP – http://msdn.microsoft.com/en-us/library/ms752914.aspx
       http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-dependency-properties-in-wpf/

AP – http://msdn.microsoft.com/en-us/library/ms749011.aspx

Advertisements

Written by nbvasu

May 12, 2009 at 9:43 pm

Posted in General, WPF

Tagged with

One Response

Subscribe to comments with RSS.

  1. great post .

    M.Jamal

    July 7, 2009 at 4:54 am


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: