WPF应用程序命令绑定不';行不通
嗨 我对WPF中的命令绑定有一个奇怪的问题。 我在对象的构造函数中添加CommandBindings。命令绑定看起来是这样的WPF应用程序命令绑定不';行不通,wpf,routed-commands,commandbinding,routedevent,Wpf,Routed Commands,Commandbinding,Routedevent,嗨 我对WPF中的命令绑定有一个奇怪的问题。 我在对象的构造函数中添加CommandBindings。命令绑定看起来是这样的 CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,Copy_Executed,Copy_Enabled)); CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut,Cut_Executed,Cut_En
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,Copy_Executed,Copy_Enabled));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut,Cut_Executed,Cut_Enabled));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,Paste_Executed,Paste_Enabled));
负责执行的共同响应函数看起来就是这样
private void Paste_Enabled(object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = selectionService != null && selectionService.CurrentSelection.Count > 0;
}
private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (GetSelected() != null)
Paste(true);
else
Paste(false);
}
private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
Copy();
}
private void Copy_Enabled(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = selectionService.CurrentSelection.OfType<DesignerItem>().Count() > 0;
}
#endregion
private void Cut_Executed(object sender, ExecutedRoutedEventArgs e)
{
Copy();
DeleteCurrentSelection(false);
}
private void Cut_Enabled(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.SelectionService.CurrentSelection.Count() > 0;
}
private void Paste_已启用(对象发送方,CanExecuteRouteEventArgs e)
{
e、 CanExecute=selectionService!=null&&selectionService.CurrentSelection.Count>0;
}
已执行私有无效粘贴(对象发送方,已执行路由目标)
{
如果(GetSelected()!=null)
粘贴(真);
其他的
粘贴(假);
}
已执行私有无效副本(对象发送方,已执行路由目标)
{
复制();
}
已启用私有无效复制(对象发送方,CanExecuteRoutedEventArgs e)
{
e、 CanExecute=selectionService.CurrentSelection.OfType().Count()>0;
}
#端区
已执行私有无效剪切(对象发送方,已执行路由目标)
{
复制();
删除当前选择(false);
}
已启用私有void Cut_(对象发送方,CanExecuteRoutedEventArgs e)
{
e、 CanExecute=this.SelectionService.CurrentSelection.Count()>0;
}
问题是只有cut命令有效。我的意思是,如果我在任何其他函数(复制或粘贴)中设置断点,则不会命中断点。有人能告诉我我做错了什么吗?应用程序窗口中的任何控件都绑定了
复制
和粘贴
命令吗?看起来UI
只查找Cut
命令,而不查找其他两个命令。确保将另外两个命令绑定到UI
上。您还需要添加按键手势
InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C
代码是否到达Copy_Enabled方法?我不会将此命令发送给任何其他控件。我只是在课堂上蹦蹦跳跳