WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel wpf wpf

WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel


The thing is that if you were following MVVM, you would have a BookViewModel for your Book model class. So you would have a INotifyPropertyChanged implementation on that view model. Exactly for that purpose MVVM exists (but not only).

That being said, the INotifyPropertyChanged has to be implemented on view model classes, not models.

UPDATE: In response to your update and our discussion in comments...

By BookViewModel I meant something else. You need to wrap in this view model not the whole collection of Book objects but an individual Book:

public class BookViewModel : INotifyPropertyChanged{    private Book book;    public Book Book {        get { return book; }        }    public string Title {        get { return Book.Title; }        set {            Book.Title = value;            NotifyPropertyChanged("Title");        }             }    public BookViewModel(Book book) {        this.book = book;    }    public event PropertyChangedEventHandler PropertyChanged;    protected void NotifyPropertyChanged(String info) {        if (PropertyChanged != null) {            PropertyChanged(this, new PropertyChangedEventArgs(info));        }    }}

And your BookProvider will return ObservableCollection<BookViewModel> instead of ObservableCollection<Book>:

public class BookProvider{    public ObservableCollection<BookViewModel> GetBooks() {        ObservableCollection<BookViewModel> books = new ObservableCollection<BookViewModel>();        books.Add(new BookViewModel(new Book {            Title = "Book1",            Authors = new List<Author> { new Author { Name = "Joe" }, new Author { Name = "Phil" } }        }));        books.Add(new BookViewModel(new Book {            Title = "Book2",            Authors = new List<Author> { new Author { Name = "Jane" }, new Author { Name = "Bob" } }        }));        return books;    }}

As you can see, when you are updating the Title property of the Book you will be doing it through the Title property of the corresponding view model that will raise the PropertyChanged event, which will trigger the UI update.


Please read this article. It explains how you can reduce code duplication by implementing INotifyPropertyChanged in the model.


Don't confuse INotifyPropertyChanged with MVVM.

Consider what INotifyPropertyChanged actually is -> It's an event that fires to say "Hey look, I've changed". If anyone cares then they can do something about it, whether they're a View, ViewModel, or whatever.

So let's start with your Book (Model). The Title property can fire a changed event, why would it not? It makes sense, the Book is dealing with it's own properties.

Now for BookViewModel - great, we don't need to duplicate the Title and bulk up our code! Whoo!

Consider a View where we want to see a list of books, or a book with a list of authors. Your ViewModel can handle additional properties specific for the view, such as IsSelected. This is a great example - why would the Book care if it was selected or not? This is the responsibility of the ViewModel.


Obviously the above depends on your architecture, but personally if I'm creating an object library I'll implement a base class with INotifyPropertyChanged and make the object properties responsible for firing the event.