title: Disable CanExecute on switching views
body: <p>I have a static property which tracks property changes in my viewmodels:</p>
  <pre><code>public static class Global
  {
     public static int Warning
     {
         get { return _warning; }
         set { _warning = value; OnStaticPropertyChanged(); }
     }
     private static int _warning;
  }
  </code></pre>
  <p>Then, in viewmodels I have CanExecute commands (for buttons to cancel changes):</p>
  <pre><code>private bool Cancel_CanExecute(object parameter)
  {
       bool changes=false;
  
       if (!string.IsNullOrEmpty(Name) &amp;&amp; !string.IsNullOrEmpty(SurName))
       { 
          changes= true;
       }
  
       if (changes)
       {
         Global.Warning = 1; //Changes are true, store this state
       }
  
       return changes;
  }
  </code></pre>
  <p>When there are changes in my views, and user want to switch a view, I want to show them a warning MessageBox. So in my main Window viewModel I have a command for changing views:</p>
  <pre><code>private void Open_view(object parameter)
  {
     if (Global.Warning != 0)
     {
         var msg = _windowservice.ShowMessage(&quot;Want to leave? Changes won't be saved.&quot;);
  
         if (msg == true) //User clicked OK in MessageBox
         {
            Global.Warning = 0; // reset static property to default, and proceed with code
         }
         else
         {
             return; //Prevent opening new view
         }
                 
      }
      //Switch to new view - Calling this line before 'Global.Warning = 0;' doesn't help either
      Open_view = new SomeViewModel();
  
      //...
  }
  </code></pre>
  <p>But, when I confirm MessageBox to leave view without saving changes &amp; with reseting static property <strong>Warning</strong> to 0, <strong>CommandManager still invokes CanExecute command of old viewmodel</strong>, so my <strong>Warning</strong> property gets again value of 1.</p>
  <p>My views are all UserControls with defined DataTemplates in resource dictionary, and only way I could manage to solve this behaviour is by UserControl code behind, like this:</p>
  <pre><code>private void UserControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
  {
       this.DataContext = null; //
  }
  </code></pre>
  <p><strong>Question: How to properly handle this situations in MVVM ?</strong> Bottom line is that I want to track property changes in my view while still be able to inform user of unsaved changes If he wants to leave this same view.</p>
  <p>EDIT: not sure If helpful, but here is my command implementation too:</p>
  <pre><code>    public class MyCommand : ICommand
      {
          private readonly Action&lt;object&gt; _execute;
          private readonly Predicate&lt;object&gt; _canExecute;
  
          public MyCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute)
          {
              _execute = execute;
              _canExecute = canExecute;
          }
  
          public bool CanExecute(object parameter)
          {
              if (_canExecute == null)
              {
                  return true;
              }
           
              return _canExecute(parameter);
          }
  
          public void Execute(object parameter)
          {
              _execute?.Invoke(parameter);
          }
  
          public event EventHandler CanExecuteChanged
          {
              add { CommandManager.RequerySuggested += value; }
              remove { CommandManager.RequerySuggested -= value; }
          }
              
  
          public void RaiseCanExecuteChanged()
          {
            CommandManager.InvalidateRequerySuggested();
          }
      }
  </code></pre>
  <p>Defined DataTemplates (resource dictionary, which is registered in app.xaml):</p>
  <pre><code>    &lt;!--Each view has It's own reference of ViewModel--&gt;
      &lt;DataTemplate DataType=&quot;{x:Type ViewModels:HomeViewModel}&quot;&gt;
          &lt;Views:HomeView /&gt;
      &lt;/DataTemplate&gt;
      //...etc..
  </code></pre>
  <p>Property for changing views:</p>
  <pre><code>        ///&lt;summary&gt;Inherited Base property&lt;/summary&gt;
          public object Open_view
          {
              get { return _open_view; }
              set { _open_view = value; OnPropertyChanged(); }
          }
          private object _open_view;
  </code></pre>
  <p>All views are opened in MainWindow via ContentControl (tried with UpdateSourceTrigger here too..) :</p>
  <pre><code> &lt;ContentControl Grid.Column=&quot;1&quot; Grid.Row=&quot;2&quot;  Content=&quot;{Binding Open_view,UpdateSourceTrigger=PropertyChanged}&quot;&gt;
  </code></pre>