title: How to use the CanExecute Method from ICommand on WPF
body: <p>How does one use the <code>CanExecute</code> Method from the <code>ICommand</code> interface?</p>
  
  <hr>
  
  <p>In my example i have a SaveCommand which i only what to be enable when the object is saveable. The XAML Code of my Savebutton looks like this:</p>
  
  <pre><code>&lt;Button Content="Save" Command="{Binding SaveCommand, Mode=TwoWay}" /&gt;
  </code></pre>
  
  <p>This is the code of my save class:</p>
  
  <pre><code>class Save : ICommand
  {
      public MainWindowViewModel viewModel { get; set; }
  
      public Save(MainWindowViewModel viewModel)
      {
          this.viewModel = viewModel;
      }
  
      public bool CanExecute(object parameter)
      {
  
          if (viewModel.IsSaveable == false)
              return false;
          return true;
      }
  
      public event EventHandler CanExecuteChanged;
  
      public void Execute(object parameter)
      {
          viewModel.Save();
      }
  }
  </code></pre>
  
  <p>The save property in the ViewModel looks like this:</p>
  
  <pre><code>    public ICommand SaveCommand
      {
          get
          {
              saveCommand = new Save(this);
              return saveCommand;
          }
          set
          {
              saveCommand = value;
          }
      }
  </code></pre>
  
  <p>This construct didn't work. The button does not enable its self when isSaveable is true.</p>