title: Can a PropertyChangedCallback be persuaded to run when the bound property's value is unchanged?
body: <p>My application uses the MVVM architecture, with the ViewModel having no knowledge of the View.  When a ViewModel object requires a new View be shown it exposes a public <code>ShowNewView</code> property that is an object whose class is based on my ViewModel base class.  The WPF View binds a custom DependencyProperty to this and uses the PropertyChangedCallback to construct and show an approperiate Window.</p>
  
  <p>This all works well the first time the <code>ShowNewView</code> property is set.  However, if the user closes the window and then attempts to re-open it, the <code>ShowNewView</code> property's value has not changed when the PropertyChanged event is raised and the PropertyChangedCallback is not invoked.</p>
  
  <p>In order to 'trick' the DependencyProperty into detecting that the value has changed (even though the value stored in the ViewModel's property may not have actually changed), I have used the <code>SetCurrentValue</code> method exposed by the Window class to force the DependencyProperty's value to <code>null</code>.</p>
  
  <pre><code>#region ShowNewViewProperty
  
  private static readonly DependencyProperty _ShowNewViewProperty =
      DependencyProperty.RegisterAttached
      (
          "ShowNewView",
          typeof(IRootViewModel),
          typeof(WpfViewWindow),
          new PropertyMetadata(ShowNewViewPropertyChanged)
      );
  
      public static DependencyProperty ShowNewViewProperty { get { return _ShowNewViewProperty; } }
  
      public static IRootViewModel GetShowNewView(Window source)
      {
          return (IRootViewModel)source.GetValue(ShowNewViewProperty);
      }
  
      public static void SetShowNewView(Window target, IRootViewModel value)
      {
          target.SetValue(ShowNewViewProperty, value);
      }
  
      private static void ShowNewViewPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          WpfViewWindow window = d as WpfViewWindow;
          IRootViewModel newValue = e.NewValue as IRootViewModel;
          if ((null != window) &amp;&amp; (null != newValue))
          {
              // Create a child WpfViewWindow.  This method is part of my
              // framework that uses ResourceDictionary entries, imported by MEF
              // to locate the View class corresponding to the ViewModel parameter's
              // class.
              WpfViewWindow modelessWindow = window.CreateWpfViewWindow(newValue);
              if (null != modelessWindow)
              {
                  // Show the new WpfViewWindow.
                  modelessWindow.Show();
              }
  
              // Clear the current value so that the next PropertyChanged event
              // is processed even if the underlying value has not actually changed.
              window.SetCurrentValue(ShowNewViewProperty, null);
          }
      }
  
      #endregion
  </code></pre>
  
  <p>Technically this works, as it results in the callback being run when the PropertyChanged event fires, regardless of whether the value has actually changed or not.  However, it results in the callback being called (recursively) twice every time the ViewModel's property is updated: once in response to the ViewModel's event and once in response to the <code>SetCurrentValue</code> method being called.</p>
  
  <p>There are a number of questions here relating to the PropertyChangedCallback not being called, or not being called more than once, in other situations.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/7339582/3581917">PropertyChangedCallback on DependencyProperty Only Firing Once</a> covers the situation where the property is a collection and the collection content changes, but the collection itself does not.  However, my property is not a collection and everything is working exactly as documented.</li>
  <li><a href="https://stackoverflow.com/q/2728267/3581917">WPF dependency property setter not firing when PropertyChanged is fired, but source value is not changed</a> looks very promising, but the answer only suggests using the callback that I am already.</li>
  </ul>
  
  <p>Is there a more elegant way to achieve this that does not result in the callback being run twice for each <code>PropertyChanged</code> event from the ViewModel?  I.e. is there some way to get around the framework's check to verify that the old and new values are different?</p>
  
  <p><strong>Clarification</strong></p>
  
  <p>The View being created isn't necessarily always a WPF Window, for example, in my unit tests it is a mock, and later in the project it may be a sperate logging assembly.  Nor are all of the ViewModel objects from the same assembly, it is known that additional functionality will be required in the future, but the specifics are currently undefined.  The application allows the user to connect a device by way of a simple network.  Initially the network is ModbusRTU over RS-485, however, the end customer may want to use CANOpen or Profinet or some other transport layer, and I have to provide a plug-in mechanism that allows the new functionality to be added without changing the existing code.</p>
  
  <p>To be fair, there are several alternative mechanisms that I could use to achieve the same result (i.e. having the ViewModel request a new View be created), but I'm interested in knowing if there is a way to make a DependencyPropety 'forget' what it's previous value was.</p>