title: How to prevent PropertyChanged from firing while controls are being initialized
body: <p>This problem has been causing headaches for a while, and it is preventing the project from going forward. Consider a WPF XAML form with controls bound to a ViewModel. (I'm using the Caliburn.Micro MVVM framework and Entity Framework for the data). An <code>Initialize()</code> method is called by the shell to load the form’s data from the database and set up PropertyChanged event handlers. There is an <code>IsDirty</code> flag that tracks whether there is changed data in the form. There is a "Save" button bound to the <code>IsDirty</code> property so that it is enabled when the data has changed.</p>
  
  <pre><code>// Sample code; forms have many controls....
  
  // this is the property that the controls are bound to
  public Entity BoundData { get; set; }
  
  public void Initialize()
  {
      // this is an example line where I query the database from the Entity Framework ObjectContext...
      BoundData = objectContext.DataTable.Where(entity =&gt; entity.ID == 1).SingleOrDefault();
  
      // this is to cause the form bindings to retrieve data from the BoundData entity
      NotifyOfPropertyChange("BoundData");
  
      // wire up the PropertyChanged event handler
      BoundData.PropertyChanged += BoundData_PropertyChanged;
  
      IsDirty = false;
  }
  
  void BoundData_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
      IsDirty = true;
  }
  
  // implementation of the IsDirty flag
  public bool IsDirty
  {
      get
      {
          return _isDirty;
      }
      set
      {
          _isDirty = value;
          NotifyOfPropertyChange("IsDirty");
      }
  }
  </code></pre>
  
  <p>The problem is that the <code>BoundData_PropertyChanged</code> event handler is hit due to the form being initialized from the database AFTER the <code>Initialize()</code> method is finished. So, the <code>IsDirty</code> flag is set to true, and the Save button is enabled, even though the form has just loaded and the user has not changed anything. What am I missing? Surely this is a common problem but I have been unable to find a good solution. This is my first MVVM project so it is entirely possible I am missing some basic concept.</p>
  
  <p><strong>UPDATE:</strong> To clarify, I believe the issue is that I need to be able to hook into an event or callback that will fire when all of the bindings are done updating, so I can then wire up the PropertyChanged event handlers.</p>