This post illustrates one of several utility classes that are in the Nito.MVVM library: SimplePropertyPath.
SimplePropertyPath is used to create a very simple binding using only INotifyPropertyChanged and not using DependencyProperty or DependencyObject. The System.Windows.Data.Binding class is much more powerful, but is dependent on the WPF-specific dependency property/object system. SimplePropertyPath only uses INotifyPropertyChanged, which gives it two advantages:
- It can be used in non-WPF environments with only minor changes, e.g., using the MVVM pattern on a compact device.
- It allows all ViewModel classes to be POCO (utilizing INotifyPropertyChanged) instead of forcing them to be derived from DependencyObject. (In my mind, at least, DependencyObject or FrameworkElement-derived classes are more of a View class).
SimplePropertyPath merely propagates an existing INotifyPropertyChanged implementation “forward” to other listeners, and propogates writes “back” to the original property. It is only capable of understanding a “simple” property path: one comprised entirely of member accessors.
Some examples will help clarify how this class can be used; the following “fake ViewModel” class is used by these examples. It just has two properties: “int Value” and “FakeVM Child”, and implements INotifyPropertyChanged:
The API of SimplePropertyPath is rather simple:
The two properties “Root” and “Path” are used to define the SimplePropertyPath. The “Value” property is used to read or write the nested property.
A simple example to start off with; to read or write the “Value” property on a FakeVM object:
Nothing too difficult there. The next example exercises reading and writing to a longer path:
Now it’s starting to act more like a real binding. Invalid property paths will result in a Value of null (writing errors to PresentationTraceSources.DataBindingSource just like WPF data binding does):
The “childmost” property is not the only one that is being monitored; SimplePropertyPath will monitor INotifyPropertyChanged for each object along the path:
Finally, SimplePropertyPath will raise its own INotifyPropertyChanged for its Value property every time it changes, whether it was caused by a change in the “childmost” property or any of the objects along the path.
SimplePropertyPath is used by the Nito.MVVM library as a building block to construct some of the more advanced classes, such as MultiProperty and MultiCommand. However, it can be useful in its own right.
[Note: all of the examples above were copied almost verbatim from the unit tests in the Nito.MVVM library].