如何将按钮绑定(bind)到 View 模型(如带有MVVM的WPF中)中的命令?
最佳答案
之前,我已经将ICommand
对象附加到Tag
和Button
对象的MenuItem
属性中。
然后,我看是否可以转换并运行它,例如:
private void button1_Click(object sender, EventArgs e)
{
ICommand command = ((Control)(sender)).Tag as ICommand;
if (command != null)
{
command.Execute();
}
}
为了更轻松地生活,请尝试对控件进行子类化(例如
Button
,MenuItem
)