我知道如何绑定命令,但我总是使用同样的方法。
创建一个实现ICommand接口的类,然后在视图模型中创建该类的新实例,绑定就可以完美运行。
这是我用于命令绑定的代码。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
testCommand = new MeCommand(processor);
}
ICommand testCommand;
public ICommand test
{
get { return testCommand; }
}
public void processor()
{
MessageBox.Show("hello world");
}
}
public class MeCommand : ICommand
{
public delegate void ExecuteMethod();
private ExecuteMethod meth;
public MeCommand(ExecuteMethod exec)
{
meth = exec;
}
public bool CanExecute(object parameter)
{
return false;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
meth();
}
}
但我希望知道基本的方法来完成这个任务,不需要第三方dll也不需要创建新的类。使用单个类执行此简单的命令绑定。实际类从ICommand接口实现并执行工作。
public ICommand Save { get { return new DelegateCommand(new Action<object>((t) => { ViewModelMethodSave(); })); } }
。对我来说,它看起来非常简单。 - askorvaga