title: Why does OnPropertyChanged not work in Code Behind?
body: <p>I'm trying to simplify some code by putting the ViewModel models into the code behind and binding the DataContext as "this", but it seems to work differently, in the following example:</p>
  
  <p><strong>Why is it when the button is clicked, the TextBlock bound to "Message" does not change, even though OnPropertyChanged("Message") is called?</strong></p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;Window x:Class="TestSimple223.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;StackPanel HorizontalAlignment="Left"&gt;
          &lt;Button Content="Button" 
                  Click="button1_Click" /&gt;
          &lt;TextBlock 
              Text="{Binding Path=Message, Mode=TwoWay}"/&gt;
          &lt;TextBlock
              x:Name="Message2"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Code Behind:</strong></p>
  
  <pre><code>using System.Windows;
  using System.ComponentModel;
  
  namespace TestSimple223
  {
      public partial class Window1 : Window
      {
          #region ViewModelProperty: Message
          private string _message;
          public string Message
          {
              get
              {
                  return _message;
              }
  
              set
              {
                  _message = value;
                  OnPropertyChanged("Message");
              }
          }
          #endregion
  
          public Window1()
          {
              InitializeComponent();
              DataContext = this;
  
              Message = "original message";
              Message2.Text = "original message2";
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              Message = "button was clicked, message changed";
              Message2.Text = "button was click, message2 changed";
          }
  
          #region INotify
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected void OnPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          } 
          #endregion
  
  
      }
  }
  </code></pre>