Saturday, 11 May 2013

A new Property System | WPF Tutorial pdf

Well, you must have surprised to to see this title. Yes, WPF comes with a completely new technique of defining a property of a control. The unit of the new property system is a Dependency property and the wrapper class which can create a Dependency property is called a DependencyObject. We use to register a Dependency property into the property system to ensure that the object contains the property in it and we can easily get or set the value of those properties whenever we like. We even use normal CLR property to wrap around a dependency property and use GetValue and SetValue to get and set values passed within it.
This is almost same as CLR property system. So what is the advantages of the new property system. Lets see the difference between Dependency property and CLR property. To Work with dependency property, you must derive the class from DependencyObject as the whole observer which holds the new Property System is defined within the DependencyObject.

Difference between Dependency property and CLR property
CLR property is just a wrapper around private variables. It uses Get / Set methods to retrieve and store value of a variable into it. So to be frank with you a CLR property gives you only one block in which you can write code to invoke whenever a property is get or set. Hence CLR property system is fairly straight forward.
On the other hand, the capabilities of Dependency property system is huge. The idea of Dependency property is to compute the value of the property based on the value of other external inputs. The external inputs might be styles, themes, system properties, animations etc. So, you can say a dependency property works with most of the WPF inbuilt features that were introduced.

Advantages of Dependency Property
As a matter of fact a Dependency Property have a lots of advantages over the normal CLR properties. Lets discuss the advantages a bit before we create our own dependency property :
1. Property Value Inheritance : By Property Value Inheritance we mean that value of a Dependency property can be overridden in the hierarchy in such a way that the value with highest precedence will be set ultimately.
2. Data Validation : We can impose Data Validation to be triggered automatically whenever the property value is modified.
3. Participation in Animation : Dependency property can animate. WPF animation has lots of capabilities to change value at an interval. Defining a
dependency property, you can eventually support Animation for that property.
4. Participation in Styles : Styles are elements that defines the control. We can use Style Setters on Dependency property.
5. Participation in Templates : Templates are elements that defines the overall structure of the element. By defining Dependency property, we can use it in templates.
6. DataBinding : As each of the Dependency property itself invokes INotifyPropertyChanged whenever the value of the property is modified, DataBinding is supported internally. To read more about INotifyPropertyChanged, please read.
7. CallBacks : You can have callbacks to a dependency property, so that whenever a property is changed, a callback is raised.
8. Resources: A Dependency property can take a Resource. So in XAML, you can define a Resource for the definition of a Dependency property.
9. Metadata overrides : You can define certain behavior of a dependency property using PropertyMetaData. Thus overriding a metadata form a derived property will not require you to redefine or reimplementing the whole property definition.
10.Designer Support : A dependency property gets support from Visual Studio Designer. You can see all the dependency properties of a control listed in the Property Window of the Designer.
In these, some of the features are only supported by Dependency Property.
Animation, Styles, Templates, Property value Inheritance etc could only be participated using Dependency property. If you use CLR property instead in such cases, the compiler will generate error.

No comments: