我有一个 ViewModel 类,我想响应从按钮触发的内置 Refresh 命令,但我不确定如何声明 CommandTarget。
简而言之,我的代码如下
ViewModel 构造函数以及 CanExecute 和 Executed 事件处理程序 -
public ViewModel()
{
CommandBinding binding = new CommandBinding(NavigationCommands.Refresh, CommandHandler);
binding.CanExecute += new CanExecuteRoutedEventHandler(binding_CanExecute);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
CommandManager.RegisterClassCommandBinding(typeof(ViewModel), binding);
}
void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Debug.Print("Refreshing...");
}
void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
标记是 -
<Button Command="Refresh">refresh</Button>
现在,我尝试将此按钮上的 CommandTarget 设置为,{Binding Source={StaticResource ViewModel}}
但我得到一个运行时说Cannot convert the value in attribute 'CommandTarget' to object of type 'System.Windows.IInputElement'
.
我是命令新手,所以我完全有可能在这里错了。任何帮助,将不胜感激。