title: WPF MVVM: ICommand Binding to controls
body: <p>I've totally lost in the command binding that is used in MVVM. How should I bind my object to the window and/or its command to the control to get method called on the <code>Button</code> <code>Click</code>?</p>
  
  <p>Here is a <code>CustomerViewModel</code> class:</p>
  
  <pre><code>public class CustomerViewModel : ViewModelBase
  {
      RelayCommand _saveCommand;
      public ICommand SaveCommand
      {
          get
          {
              if (_saveCommand == null)
              {
                  _saveCommand = new RelayCommand(param =&gt; this.Save(), param =&gt; this.CanSave);
                  NotifyPropertyChanged("SaveCommand");
              }
              return _saveCommand;
          }
      }
  
      public void Save()
      {
          ...
      }
  
      public bool CanSave { get { return true; } }
  
      ...
  </code></pre>
  
  <p><code>ViewModelBase</code> implements the <code>INotifyPropertyChanged</code> interface
  Here is how <code>Button</code> is bound to the command:</p>
  
  <pre><code>&lt;Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" /&gt;
  </code></pre>
  
  <p>An instance of the <code>CustomerViewModel</code> is assigned to the <code>DataContext</code> of the window that contains a <code>Button</code>.</p>
  
  <p>The given example is not working: I've put break point into the <code>Save</code> method but execution doesn't pass to the method. I've saw a lot of examples (on the stackoverflow too), but can't figure out how binding should be specified.</p>
  
  <p>Please advise, any help will be appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>P.S. Probably I need to specify <code>RelativeSource</code> in the Button binding... something like this:</p>
  
  <pre><code> Command="{Binding Path=DataContext.Save, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
  </code></pre>
  
  <p>but which type should be specified for ancestor?</p>