Encapsulating common functionality
Probably the most commonly used interface in any WPF application would be the INotifyPropertyChanged
interface, as it is required to correctly implement data binding. By providing an implementation of this interface in our base class, we can avoid having to repeatedly implement it in every single View Model class. It is therefore, a great candidate for inclusion in our base class. There are a number of different ways to implement it depending on our requirements, so let's take a look at the most basic first:
public virtual event PropertyChangedEventHandler PropertyChanged; protected virtual void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
In all forms of this implementation, we first need to declare the PropertyChanged
event. This is the event that will be used to notify the various binding sources and targets of changes to the data bound...