Friday, August 30, 2013

Difference between the dependency property and clr-property

When you play with WPF , you have to deal with very common and effective feature of WPF ie. Dependency property. the Use of this feature is very simple but concept behind is very much complex.

While using the dependency property a question is arises that why we need a new property system against normal property.

In the Normal property the value of a normal .NET property is read directly from a private member in your class

The dependency property is a new kind of implementation of properties that automatically notify any registered interested party each time the value of the property changes.
means if you bind a number of controls to the dependency properties, your controls will surely notified as the value of the bound properties change, without writing any additional code.

 The dependency property has significant amount of added value.The dependency property is the basis of many WPF key features as-
  • Animation
  • Data Binding
  • Styles
  • Resources
  • Metadata overrides
  • Property value inheritance
  • WPF Designer integration
refer the link http://msdn.microsoft.com/en-us/library/ms752914.aspx
 
most of properties that are exposed by WPF elements are dependency properties.it is used without realizing it because it is designed to be consumed in the same way as normal property.
The dependency properties in the WPF libraries are always wrapped by ordinary .Net property, so it can be usable in the normal way.
  The value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.
When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

the wrapper class which can create a Dependency property is called a DependencyObject

Advantage:
This free, painless and automatic implementation of the observer pattern is tremendously powerful and greatly reduces the burden on the client programmer (in fact, the data-binding system depends on it!).

Efficient Storage/Reduce memory footprint

We know that in UI design many of the properties of the control will remain with its initial value.
Dependency properties store only modified properties in the instance, so less memory is used to store property values
If the value of a property has not been store locally,WPF will retrieve its value from a style, another element, or the default value

To understand this let consider a example-
Suppose we have a number of buttons in a form, each button has a dozen of properties,which, , if they set through one of these mechanisms, use no memory at all. other wise with these mechanism for each property take memory to set with control weather it is not set locally.

Dynamic value resolution

The actual value of a dependency property as seen by the object may be set by many source.WPF take many factors into consideration.

The behavior gives dependency property their name, a dependency property depends on the multiple property providers.each with its own level of precedence.

When we retrieve a value from a property value, the WPF property system goes through a series of steps to arrive at the final value.
Following are the factors ,arranged from lowest to highest precedence.

Below is the list of precedence from lowest priority to highest priority .

-Default Value: The default value (as set by FrameworkPropertyMetadata object).
it is used in the case that no other value has been applied

-Inherited Value: A Property might inherit a value from a parent element.
e.g. DataContext which is inherited unless specifically set.
-Default style: The value set from a theme style
e.g. Border and text styles for buttons or text boxes will use a theme style.
- Style setters. Styles contained by the, page, application or animations use setters to modify the value of a dependency property.

-Template triggers: A property may be set by a template depending upon a MouseOver or Focused state.

-Style triggers: Similarly, styles can specify triggers that apply styles according to the value of a property. Maybe a bank balance value would be shown in red if the account was overdrawn.

-Implicit Styles: These styles are defined in the resources of the object or page that uses them and override default styles which are of lower precedence.

-TemplatedParent template properties: Dependency properties which are owned by items in the visual tree and which exist because they have been created as the result of a template setting are modified using this principle. You will not declare anything explicitly for this but rather it is inferred by the action of the template system.

-Local value: Explicitly set by markup or a SetValue call in code.

-Animation: Properties set by animation storyboard setters.

-Coersion: This is the highest priority exception case which is to allow you to alter the value of a property despite every other attempt. Some properties are coerced automatically but in general, you will have to respond to a callback and set the value in code to override all the other possibilities. Basically, coersion is a get out clause in which you can say "I don't care what you think. Do it my way!" To do this you'll use a CoerceValueCallback.

Change Notification

Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding.

Property Value Inheritance
 When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value.