我完全迷失在MVVM中使用的命令绑定中。我应该如何将我的对象绑定到窗口和/或其命令绑定到控件以获取对
Button
Click
?
这里是一个
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; } }
...
ViewModelBase
实现
INotifyPropertyChanged
界面
以下是如何
纽扣
绑定到命令:
<Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" />
的实例
客户视图模型
分配给
DataContext
包含一个
纽扣
.
给出的示例不起作用:我已将断点放入
Save
方法,但执行不会传递给该方法。我已经看到了很多示例(在stackoverflow上也看到了),但无法确定应该如何指定绑定。
请告知,如有任何帮助将不胜感激。
谢谢。
P.S.可能我需要说明
RelativeSource
在按钮绑定中…像这样:
Command="{Binding Path=DataContext.Save, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
但是应该为祖先指定哪种类型?