Tuesday 24 May 2011

Dependency Objects vs INotifyPropertyChanged

WPF and Silverlight user interfaces are all about data binding. 

To allow your POCO (plain old CLR objects) to take advantage of two way data binding, your object needs to raise the change event.  This can be done by either specialising from DependencyObject or by implementing the INotifyPropertyChanged interface.

Here is a simple example of both in action.

DependencyObject

INotifyPropertyChanged

You can then bind your UI controls to your objects by setting the DataContext and ItemsSource of the control:

Now you may have noticed I’ve added some validation for the Age property.  In my next blog article I’ll show why I find INotifyPropertyChanged a more flexible solution and gives the ability for contextual validation.

No comments:

Post a Comment