Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how DependencyProperty works and is implemented

The Silverlight/WPF DependencyProperty enables data binding and indicates when the property has changed in value, without implementing INotifyPropertyChanged. My question is on how does this work at the low level - how does DependencyProperty or DependencyObject perform this change notification when neither DependencyObject, DependencyProperty, nor DispatcherObject define any events. Would this have something to do with the DispatcherObject.Dispatcher property?

Dependency properties, or the DependencyObject class, do not natively support INotifyPropertyChanged for purposes of producing notifications of changes in DependencyObject source property value for data binding operations.

This excellent clarification was taken word-for-word from:

http://msdn.microsoft.com/en-us/library/ms752914.aspx

http://msdn.microsoft.com/en-us/library/ms753358.aspx

like image 279
T. Webster Avatar asked Aug 16 '11 14:08

T. Webster


People also ask

How does dependency property work?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.

What is DependencyProperty C#?

A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the built-in feature of providing notification when the property has changed, data binding and styling.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.

What are the advantages of dependency properties?

Dependency properties provide a clean API for change tracking as well as validation. Dependency properties provide property value inheritance. Dependency properties provide great performance benefits when data bound.


1 Answers

Dependency properties are tightly integrated with the binding system internally. So instead of "notifying that the property changed", the code that sets the dependency property can call directly into the binding system and tell it to updated.

Similarly, things like inherited/attached properties can be updated on any descendant elements and/or the layout/measure/arrange can be updated. It can even tell any triggers (in Styles or ControlTemplates) to be reevaluated.

The Dispatcher isn't really related, but may be used during the process.

So in short, it's baked into WPF/Silverlight.

like image 66
CodeNaked Avatar answered Oct 18 '22 05:10

CodeNaked