The ViewModel sits between the View and the Model. When a change in the ViewModel occurs, the View must be notified. The mechanism for this is the INotifyPropertyChanged interface that defines an event that the controls in the View subscribe to. There is really no magic going on here at all—every ViewModel must implement this interface and the best place to do it would be in the ViewModel base class. Follow these steps:
- In the News project, open up ViewModels.cs.
- Add the following code in bold:
public abstract class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
This implements the INotifyPropertyChanged interface. The next step is all about reducing the number of lines of code that we will have to write. Normally, you would have to manually raise the PropertyChanged event from your own code, but thanks to IL weavers that inject code at build time, we simply have to create normal...