首页 新闻 会员 周边

WPF 中自定义快捷键

0
[已解决问题] 解决于 2013-01-22 09:50

界面上放一个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就执行我的代码了这个是怎么回事?

WPF
BoyLife的主页 BoyLife | 初学一级 | 园豆:18
提问于:2012-07-09 21:24
< >
分享
最佳答案
0
Key.P | Key.A 这是或 位运算符号,你是不是想 key.P&Key.A
奖励园豆:5
Lee's Blog | 小虾三级 |园豆:530 | 2012-07-21 09:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册