title: WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel
body: <p>I have read a number of debates on where to implement INotifyPropertyChanged here on StackOverflow and other blogs but it seems that there are cases where you have to implement it on the Model.  Here is my scenario - I am looking for feedback on my conclusion or is my approach wrong.</p>
  
  <p>I am using this implementation of an ObservableDictionary (<a href="http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/" rel="noreferrer">ObservableDictionary</a>) because I need performant queries using the key.</p>
  
  <p>In this dictionary I place the collection of Model objects.</p>
  
  <p>In my VM, I declare an instance (Books) of the dictionary and in the XAML bind to it.</p>
  
  <pre><code>    &lt;tk:DataGrid AutoGenerateColumns="False" Grid.Row="1" ItemsSource="{Binding Mode=TwoWay, Path=Books.Store}" Grid.ColumnSpan="2" Margin="3"&gt;
          &lt;tk:DataGrid.Columns&gt;
              &lt;tk:DataGridTextColumn Binding="{Binding Mode=TwoWay, Path=Value.Name}" MinWidth="100" Header="Name" /&gt;
              &lt;tk:DataGridTextColumn Binding="{Binding Mode=TwoWay, Path=Value.Details}" MinWidth="300" Header="Details" /&gt;
          &lt;/tk:DataGrid.Columns&gt;        
      &lt;/tk:DataGrid&gt;  
  </code></pre>
  
  <p>If I implement INotifyPropertyChanged on the VM for Books and change the value of a Book name in code, the UI is not updated.</p>
  
  <p>If I implement INotifyPropertyChanged on the VM for Store and change the value of a Book name in code, the UI is not updated.</p>
  
  <p>If I implement INotifyProperyChanged on the Model and change the value a Book name in code, the UI is updated.</p>
  
  <p>The Changed event is not fired in the first case because the Dictionary setter is not called, it's Item (a Book) is.</p>
  
  <p>Am I missing something because if this is the correct interpretation, if I want consistent notifications for my Models regardless of whether they are bound to directly from XAML or via some sort of collection, I would always want the Model to implement INotifyProperyChanged.  </p>
  
  <p>Btw, besides the dll reference, I personally do no see INotifyPropertyChanged as a UI function - think it should be defined in a more general .net namespace - my 2 cents.</p>
  
  <p><strong>EDIT STARTS HERE:</strong></p>
  
  <p>We were having such a good semantics debate that I missed the core of my question so here it is posted again but with a very simple MVVM example illustrate my question.</p>
  
  <p><strong>The Models:</strong></p>
  
  <pre><code>public class Book
  {
      public string Title { get; set; )
      public List&lt;Author&gt; Authors { get; set; }
  }
  
  public class Author
  {
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><strong>The Data Provider to generate some dummy data</strong></p>
  
  <pre><code>public class BookProvider
  {
      public ObservableCollection&lt;Book&gt; GetBooks() {
          ObservableCollection&lt;Book&gt; books = new ObservableCollection&lt;Book&gt;();
  
          books.Add(new Book {
              Title = "Book1",
              Authors = new List&lt;Author&gt; { new Author { Name = "Joe" }, new Author { Name = "Phil" } }
          });
  
          books.Add(new Book {
              Title = "Book2",
              Authors = new List&lt;Author&gt; { new Author { Name = "Jane" }, new Author { Name = "Bob" } }
          });
  
          return books;
      }
  }
  </code></pre>
  
  <p><strong>The ViewModel</strong></p>
  
  <pre><code>    public class BookViewModel : INotifyPropertyChanged
  {
      private ObservableCollection&lt;Book&gt; books;
      public ObservableCollection&lt;Book&gt; Books {
          get { return books; }
          set {
              if (value != books) {
                  books = value;
                  NotifyPropertyChanged("Books");
              }
          }
      }
  
      private BookProvider provider;
  
      public BookViewModel() {
          provider = new BookProvider();
          Books = provider.GetBooks();
      }
  
      // For testing the example
      public void MakeChange() {
          Books[0].Title = "Changed";
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void NotifyPropertyChanged(String info) {
          if (PropertyChanged != null) {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  }
  </code></pre>
  
  <p><strong>XAML code behind</strong>
  Would not normally to it this way - just for simple example</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      private BookViewModel vm;
  
      public MainWindow() {
          InitializeComponent();
  
          vm = new BookViewModel();
          this.DataContext = vm;
      }
  
      private void Button_Click(object sender, RoutedEventArgs e) {
          vm.MakeChange();
      }
  }
  </code></pre>
  
  <p><strong>The XAML</strong></p>
  
  <pre><code>&lt;Window x:Class="BookTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="242*" /&gt;
          &lt;RowDefinition Height="69*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;ListBox ItemsSource="{Binding Books}"&gt;
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel Orientation="Vertical"&gt;
                      &lt;TextBlock Text="{Binding Title}" /&gt;
                      &lt;ListBox ItemsSource="{Binding Authors}"&gt;
                          &lt;ListBox.ItemTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding Name}" FontStyle="Italic" /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/ListBox.ItemTemplate&gt;
                      &lt;/ListBox&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;
      &lt;/ListBox&gt;
      &lt;Button Grid.Row="1" Content="Change" Click="Button_Click" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>As coded above, when I click on the button and change the value in the first Book, the UI does not change.</p>
  
  <p>However, when I move the INotifyPropertyChanged to the Model it works fine (UI updates) because the change is in the Model property setter not Books in the VM:</p>
  
  <pre><code>public class Book : INotifyPropertyChanged
  {
      private string title;
      public string Title {
          get { return title; }
          set {
              if (value != title) {
                  title = value;
                  NotifyPropertyChanged("Title");
              }
          }
      }
  
      public List&lt;Author&gt; Authors { get; set; }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void NotifyPropertyChanged(String info) {
          if (PropertyChanged != null) {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  }
  </code></pre>
  
  <p>So back to my original question, how do I accomplish this without implementing INotifyPropertyChanged in the Model?</p>
  
  <p>Thanks.</p>