01 November 2009

Dependency property

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a common language runtime (CLR) property. Collectively, these services are typically referred to as the WPF property system. A property that is backed by the WPF property system is known as a dependency property.
To be more clear this is same as .NET normal property system, regardless it can hold default value with built in validation sytem.
Intrduction of dependency property, Microsoft has achieved following things.
Value Inheritance
Nothing but the ability of a .NET element to inherit the value. If no local value is set, the dependency property navigates up the logical tree until it finds a value.to be more clear I have root element and have 5 text box on that, and if i say the font defanition on root level all text box will take up that font, unless it is not defined at text box lev el.
Reduced memmory
Yes.It is going to save lot of memory.For example I am defining a button, it has round about 100 properties asssociated with this.Dependency property enable us not to define this 100 properties at element level as it has the capability of inherit the value from parent levels.
Change notification
Dependency properties have a built-in change notification mechanism.


new FrameworkPropertyMetadata( string,
OntextPropertyChanged,
OnCoerceTextProperty ),
OnValidateTextProperty );


Please see the example:


public static readonly DependencyProperty IsSpinningProperty =
DependencyProperty.Register(
"IsSpinning", typeof(Boolean),


...


);
public bool IsSpinning
{
get { return (bool)GetValue(IsSpinningProperty); }
set { SetValue(IsSpinningProperty, value); }
}