Thursday, November 3, 2016

Dependencies properties

WPF vastly uses dependency properties internally. For example button’s background property is backed by a dependency property called BackgroundProperty.

Dependency property’s name always ends with property.

Dependency properties are always declared as static and read-only because it should be available to all the controls hence it should be shared and therefore it should always static.

It should be read-only because it’s value should not be changed after initialization.
Data binding can be done with dependency properties only.

Dependency properties are special properties which provide change notification.

Why We Need Dependency Properties

Dependency property gives you all kinds of benefits when you use it in your application. Dependency Property can used over a CLR property in the following scenarios −
If you want to set the style
If you want data binding
If you want to set with a resource (a static or a dynamic resource)
If you want to support animation

Basically, Dependency Properties offer a lot of functionalities that you won’t get by using a CLR property.

The main difference between dependency properties and other CLR properties are listed below −

CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.

Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well.

No comments: