命令绑定不起作用? [英] commandbinding not working?
问题描述
我正在开发一个多选项卡应用程序(例如:多选项卡文本编辑器),其中每个 tabitem 都有自己的内容.在 tabitem 的 contextmenu 中,它们是带有命令的 menuitem,比如 SelectAll 命令.
I working on a multi-tab application (For Ex: Multi-Tab Text Editor), where each tabitem has its own content. And in contextmenu of tabitem, their is menuitem with a command, say SelectAll command.
运行应用程序后,菜单项始终处于禁用状态,不执行任何命令.
After running app, the menu item is always disabled, no command execution is done.
那么,我怎样才能使我的命令绑定工作?
So, how can i make my commandbindings work ?
代码::
在 TextEditor 的上下文菜单中>
In Context Menu At TextEditor>
<MenuItem Command="local:TextEditor.SelectAllCommand" Header="Select All" />
在 TextEditor 的 CommandBindings 中>
In CommandBindings At TextEditor>
<UserControl.CommandBindings>
<CommandBinding Command="local:TextEditor.SelectAllCommand"
Executed="SelectAll_Executed" CanExecute="SelectAll_CanExecute" />
</UserControl.CommandBindings>
带有 TextEditor 的 TabItems 是在运行时创建的
The TabItems with TextEditor are created at run time
推荐答案
发生这种情况是因为 ContextMenus 是具有自己的 VisualTree 和 LogicalTree 的独立窗口.
This happens since the ContextMenus are separate windows with their own VisualTree and LogicalTree.
这样使用
<MenuItem Header="Cut" Command="Cut" CommandTarget="
{Binding Path=PlacementTarget,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ContextMenu}}}"/>
更多内容请点击下方链接
For more check the link below
http://www.wpftutorial.net/RoutedCommandsInContextMenu.html
这篇关于命令绑定不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!