title: How to use INotifyPropertyChanged correctly in WPF/XAML
body: <p>I have a custom object that I am trying to bind to a control. On that custom object I have implemented the INotifyPropertyChanged interface. I have successfully bound to my object and a property on that object.</p>
  
  <p>What I can't figure out is how to go from there. I've been working on this for 2 days now and I still cannot get it working.</p>
  
  <p>My assumption was that when I would change the property bound to the control that the value set in that property would then show up in the control. However, no matter how much I change the property, the UI is never updated beyond it's initial value.</p>
  
  <p>I have implemented INotifyPropertyChanged in this manner:
  <a href="http://joshsmithonwpf.wordpress.com/2007/08/29/a-base-class-which-implements-inotifypropertychanged/" rel="nofollow noreferrer">A base class which implements INotifyPropertyChanged</a></p>
  
  <p>So my base class is this:</p>
  
  <pre><code>[Serializable]
  public abstract class BindableObject : INotifyPropertyChanged
  {
      #region Data
  
      private static readonly Dictionary&lt;string, PropertyChangedEventArgs&gt; eventArgCache;
      private const string ERROR_MSG = "{0} is not a public property of {1}";
  
      #endregion // Data
  
      #region Constructors
  
      static BindableObject()
      {
          eventArgCache = new Dictionary&lt;string, PropertyChangedEventArgs&gt;();
      }
  
      protected BindableObject()
      {
      }
  
      #endregion // Constructors
  
      #region Public Members
  
      /// &lt;summary&gt;
      /// Raised when a public property of this object is set.
      /// &lt;/summary&gt;
      [field: NonSerialized]
      public event PropertyChangedEventHandler PropertyChanged;
  
      /// &lt;summary&gt;
      /// Returns an instance of PropertyChangedEventArgs for 
      /// the specified property name.
      /// &lt;/summary&gt;
      /// &lt;param name="propertyName"&gt;
      /// The name of the property to create event args for.
      /// &lt;/param&gt;  
      public static PropertyChangedEventArgs
          GetPropertyChangedEventArgs(string propertyName)
      {
          if (String.IsNullOrEmpty(propertyName))
              throw new ArgumentException(
                  "propertyName cannot be null or empty.");
  
          PropertyChangedEventArgs args;
  
          // Get the event args from the cache, creating them
          // and adding to the cache if necessary.
          lock (typeof(BindableObject))
          {
              bool isCached = eventArgCache.ContainsKey(propertyName);
              if (!isCached)
              {
                  eventArgCache.Add(
                      propertyName,
                      new PropertyChangedEventArgs(propertyName));
              }
  
              args = eventArgCache[propertyName];
          }
  
          return args;
      }
  
      #endregion // Public Members
  
      #region Protected Members
  
      /// &lt;summary&gt;
      /// Derived classes can override this method to
      /// execute logic after a property is set. The 
      /// base implementation does nothing.
      /// &lt;/summary&gt;
      /// &lt;param name="propertyName"&gt;
      /// The property which was changed.
      /// &lt;/param&gt;
      protected virtual void AfterPropertyChanged(string propertyName)
      {
      }
  
      /// &lt;summary&gt;
      /// Attempts to raise the PropertyChanged event, and 
      /// invokes the virtual AfterPropertyChanged method, 
      /// regardless of whether the event was raised or not.
      /// &lt;/summary&gt;
      /// &lt;param name="propertyName"&gt;
      /// The property which was changed.
      /// &lt;/param&gt;
      protected void RaisePropertyChanged(string propertyName)
      {
          this.VerifyProperty(propertyName);
  
          PropertyChangedEventHandler handler = this.PropertyChanged;
          if (handler != null)
          {
              // Get the cached event args.
              PropertyChangedEventArgs args =
                  GetPropertyChangedEventArgs(propertyName);
  
              // Raise the PropertyChanged event.
              handler(this, args);
          }
  
          this.AfterPropertyChanged(propertyName);
      }
  
      #endregion // Protected Members
  
      #region Private Helpers
  
      [Conditional("DEBUG")]
      private void VerifyProperty(string propertyName)
      {
          Type type = this.GetType();
  
          // Look for a public property with the specified name.
          PropertyInfo propInfo = type.GetProperty(propertyName);
  
          if (propInfo == null)
          {
              // The property could not be found,
              // so alert the developer of the problem.
  
              string msg = string.Format(
                  ERROR_MSG,
                  propertyName,
                  type.FullName);
  
              Debug.Fail(msg);
          }
      }
  
      #endregion // Private Helpers
  }
  </code></pre>
  
  <p>I inherit from that class above and in my derived class I do this on my property:</p>
  
  <pre><code>    public virtual string Name
      {
          get
          {
              return m_strName;
          }
          set
          {
              m_strName = value;
              RaisePropertyChanged("Name");
          }
      }
  </code></pre>
  
  <p>My XAML looks like this (abbreviated version):</p>
  
  <pre><code>&lt;Window x:Class="PSSPECApplication.Windows.Project"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:System="clr-namespace:System;assembly=mscorlib"
      DataContext="{Binding SizingProject, RelativeSource={RelativeSource Self}}"&gt;
          &lt;StackPanel VerticalAlignment="Center"&gt;
              &lt;TextBox Name="txtProjectName" Text="{Binding Name}" /&gt;
          &lt;/StackPanel&gt;
  </code></pre>
  
  <p>You can see that the window's data context is a property called SizingProject. SizingProject is of the derived type (derived from BindableObject) and has the Name property in it and Raises the PropertyChanged event handler.</p>
  
  <p>In my window's constructor I populate SizingProject and it's Name property is set.</p>
  
  <p>To test this I also have a button on the window that fires an event that sets the Name property to something other than what it is originally. However when the name property is changed, nothing ever happens. I have traced back to the BindableObject and the PropertyChanged event is always set to null, so no handler is ever set and run. Why is this?</p>
  
  <p>I thought by implementing INotifyPropertyChanged and using that type in a binding forces WPF to set that event handler automatically and then the correct behavior happens? For me, I never see that behavior.</p>
  
  <hr>
  
  <p>I figured out the issue. What I needed to do was to create a DependencyProperty for my property SizingProject. After I did that, everything worked fine.</p>
  
  <pre><code>        public static readonly DependencyProperty SizingProjectProperty =
          DependencyProperty.Register("SizingProject", typeof(Sizing.Project), typeof(Project), new UIPropertyMetadata());
  
      public Sizing.Project SizingProject
      {
          get
          {
              return (Sizing.Project)GetValue(Project.SizingProjectProperty);
          }
          set
          {
              SetValue(Project.SizingProjectProperty, value);
          }
      }
  </code></pre>