界面上放一个TreeView 为它的一级节点添加ContextMenu 我想为ContextMenu中的MenuItem设置快捷键。
class HotKeysCommands { private static RoutedUICommand _GroupAdd = new RoutedUICommand(); /// <summary> /// 新建分组 /// </summary> public static RoutedUICommand GroupAdd { get { return HotKeysCommands._GroupAdd; } } } TreeViewItem treeitem=new TreeViewItem () ContextMenu contextGroupMenu = new ContextMenu(); MenuItem menuItem = new MenuItem(); menuItem.InputGestureText = “Alt+G+A”; menuItem.Command = HotKeysCommands.GroupAdd KeyBinding keybin = new KeyBinding(); CommandBinding com = new CommandBinding(); keybin.Gesture = new KeyGesture(Key.P | Key.A, ModifierKeys.Control); keybin.Command = HotKeysCommands.GroupAdd; com.Command = HotKeysCommands.GroupAdd; treeitem.InputBindings.Add(keybin); treeitem.CommandBindings.Add(HotKeysCommands.GroupAdd); contextGroupMenu.items.add(menuItem ); treeitem.ContextMenu=contextGroupMenu ;
这是我的部分代码。可是我这样写并不正确 我按了Ctrl+P还没按A就执行我的代码了这个是怎么回事?
Key.P | Key.A 这是或 位运算符号,你是不是想 key.P&Key.A