khrabrovart
khrabrovart

Reputation: 179

How to implement Property Change Notification

In MVVM how should I implement INotifyPropertyChanged interface: in a ViewModel class or in a Model class? How to handle model's property changed event if INotifyPropertyChanged interface has been implemented in a ViewModel?

Upvotes: 1

Views: 425

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61369

First, you always implement it in your View Model, because that interface is used by the framework to update the UI when you updated data in the view model.

You can implement it in the Model, but it is by no means required. If the Model is changing out from under you, you could, and likely should, easily raise your own (semantically clearer) events that the View Model listens to in order to update its data.

The actual implementation should look like this (MSDN):

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Upvotes: 3

Related Questions