I completely lost the command binding that is used in MVVM. How to associate my object with a window and / or its command with a control to get the method called in Button Click?
Here is the class CustomerViewModel:
public class CustomerViewModel : ViewModelBase
{
RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave);
NotifyPropertyChanged("SaveCommand");
}
return _saveCommand;
}
}
public void Save()
{
...
}
public bool CanSave { get { return true; } }
...
ViewModelBaseimplements the interface INotifyPropertyChanged
Here is how it is Buttonattached to the command:
<Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" />
An instance is CustomerViewModelassigned to the DataContextwindow containing Button.
This example does not work: I put a breakpoint in the method Save, but execution does not go to the method. I have seen many examples (also on stackoverflow), but I cannot figure out how to specify a binding.
Please advise any help would be appreciated.
Thank.
P.S. , RelativeSource Button... - :
Command="{Binding Path=DataContext.Save, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
?