title: C#, WPF, MVVM and INotifyPropertyChanged
body: <p>I'm getting confused; I thought I understood INotifyPropertyChanged.</p>
  
  <p>I have a small WPF app with a frontend MainWindow class, a viewmodel in the middle and a model at the back.  </p>
  
  <p>The model in my case is the Simulator class.  </p>
  
  <p>The SimulatorViewModel is nearly transparent and just interfaces properties between MainWindow and Simulator.  </p>
  
  <p>Simulator implements INotifyPropertyChanged and each property setter in Simulator calls the RaisePropertyChanged method:  </p>
  
  <pre><code>private string serialNumber;
  public string SerialNumber
  {
      get { return serialNumber; }
      set
      {
          serialNumber = value;
          RaisePropertyChanged("SerialNumber");
      }
  }
  
  public event PropertyChangedEventHandler PropertyChanged;
  
  public void RaisePropertyChanged(string propName)  
  {  
      if (this.PropertyChanged != null)  
      {  
          this.PropertyChanged(this, new PropertyChangedEventArgs(propName));  
      }  
  }    
  </code></pre>
  
  <p>In the xaml, I have a TextBox with binding like this:</p>
  
  <pre><code>Text="{Binding Path=SerialNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  </code></pre>
  
  <p>and the DataContext is the SimulatorViewModel (but see my comment about changing the DataContext to the model)</p>
  
  <p>The ViewModel just passes the properties through:   </p>
  
  <pre><code>public string SerialNumber  
  {  
      get { return Simulator.SerialNumber; }  
      set { Simulator.SerialNumber = value; }  
  }  
  </code></pre>
  
  <p>Programmatic updates to the property SerialNumber in Simulator are not propagating up to the UI although curiously the initial value, set in the Simulator constructor, is getting there.</p>
  
  <p>If I break on the SerialNumber setter in the Simulator and follow into RaisePropertyChanged, I find that PropertyChanged is null and so the event is not propagating upwards to the GUI.</p>
  
  <p>Which leads me to several questions:</p>
  
  <ol>
  <li><p>What exactly should be hooking into the PropertyChanged event? I'm looking to be more specific than just "WPF". What's the link between that event and the Binding statement in the xaml?</p></li>
  <li><p>Why is the initial property value getting up to the UI at startup, but not subsequently?</p></li>
  <li><p>Am I right to have Simulator (the model) implementing INotifyPropertyChanged, or should it be the ViewModel doing that? If the ViewModel does it, then programmatic changes in the model don't trigger PropertyChanged, so I'm unclear on the correct pattern. I realise my ViewModel is virtually redundant, but that's due to the simplicity of the project; a more complex one would make the ViewModel concept work harder. My understanding is that the ViewModel is the place to interface my unit tests.</p></li>
  </ol>