题
我有一个模型类,我想响应该建立在刷新命令。是发射一个按钮,但我不知道如何申报的CommandTarget.
简单地说,我的代码如下
视图模型的构造和项和执行活动的处理程序-
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'
.
我是新来的命令,因此这是完全有可能我有各类错误在这里。Anyhelp将不胜感激。
解决方案
RoutedCommand
s。不混合。 RoutedCommand
s绑的视觉树和依靠WPF的 CommandBindings
收集。你应该实现自己的 ICommand
类工作。模式。看看 棱镜的实现 对于初学者。
在我自己的。项目,我有几个命令的实现:
DelegateCommand
.电话提供的代表,以确定是否命令可以执行,并为执行该命令。ActiveAwareCommand
.工作结合的一个接口(IActiveAware
)和发送命令执行的当前活动的项目。多个活动的注意实现登记册本身的命令,该命令自动路线CanExecute
/Execute
呼吁当前活动的项目。
不隶属于 StackOverflow