如何将Ctrl +,(control plus逗号)指定为WPF菜单项的键盘快捷键?

Ler*_*rve 2 c# wpf keyboard-shortcuts

我想将键盘快捷键Ctrl+ ,(控制加逗号)分配给"首选项..."菜单项.我怎么做?

有一个Key.OemCommaKey枚举.我使用Key.OemComma如下面的代码示例所示.这在功能上非常好.但是GUI-wise:菜单项显示为

  • 首选项(Ctrl + OemComma)

代替

  • 首选项(Ctrl +,)

示例代码

InputGestureCollection keyInputs = new InputGestureCollection();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 6

我认为采用显示字符串的KeyGesture构造函数可以工作.你可以这样称呼它:

InputGestureCollection keyInputs = new InputGestureCollection();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control, "Ctrl+,"));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);
Run Code Online (Sandbox Code Playgroud)