MVVM Part 2: Property Changing

Note: This posting is part of a series. See MVVM-Library for other parts and download.

Remember that ViewModels are databound to the View?

How are they bound?

You might know that the binding engine in WPF relies on INotifyPropertyChanged for that:

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.

Property Names as strings…

PropertyChanged expects an instance of PropertyChangedEventArgs which provides a property PropertyName containing the name of the property that has changed as a string.

Thinking about my recently published posting Ban string literals from your code! you might be able to comprehend why I not really like that way: You can easily misspell the property name.

My solution

Class Diagram

Class Diagram

The classes

Property

The class Property represents a property (quite logical, isn’t it ;-)) with all its properties: The type that declares it, its name, its returning type and a callback that returns it value for an passed object.

It is the core of this project part.

Properties are immutable objects that are created via the static Factory-Method Create. You can either pass a PropertyInfo object or a Func<T, object> (speaking strictly a Expression<Func<T, object>>) which returns the value of the property. (Example later in this posting).

KnownPropertyChangedEventArgs

KnownPropertyChangedEventArgs is an EventArgs class that inherits PropertyChangedEventArgs and adds the property Property returning a Property 🙂 object.

INotifyKnowPropertyChanged

INotifyKnowPropertyChanged implements INotifyPropertyChanged and hides its PropertyChanged event. Instead it declares an event with the same name but of type EventHandler<KnownPropertyChangedEventArgs>.

So INotifyKnowPropertyChanged also informs about property changes. However, it identifies the changed property not via a simple string but with an object of the described Property class.

NotifyKnownPropertyChanged

NotifyKnownPropertyChanged provides a default implementation of the INotifyKnowPropertyChanged interface.

It maximizes comfort by providing two different methods:

  • OnPropertyChanged: Derived classes will call this method in order to notify about property changes.
  • NotifyPropertyChanged: This method will be called whenever a property changes (respectively when OnPropertyChanged is called). You can override it in order to be notified about property changes.

Sample project

The sample project today is quite small: It’s a simple console application to show how to use those classes. You can find it at MVVM-Library.

Code Snippet

I created a small snippet that helps declaring properties using this way of property change-tracking.

Your opinion

What do you think?

Any bugs / issues / suggestions?

Looking forward to your comments!

Advertisements
Posted in My interpretation of MVVM, WPF. Tags: , , , , , , . Comments Off on MVVM Part 2: Property Changing
%d bloggers like this: