2009-07-29 63 views

回答

2

在这里,您可以与您正在寻找的命令替换ApplicationCommands.Copy

foreach (KeyBinding binding in InputBindings) 
{ 
    if (binding.Command == ApplicationCommands.Copy) 
    { 
     MessageBox.Show(binding.Modifiers.ToString() + " + " + binding.Key.ToString()); 
    } 
} 
-2

使用的键绑定 - http://msdn.microsoft.com/en-us/library/ms752308.aspx

<Window.InputBindings> 
    <KeyBinding Key="C" 
      Modifiers="Control" 
      Command="ApplicationCommands.Copy" /> 
</Window.InputBindings> 
+1

不,他问你如何获得键盘快捷键,而不是如何添加KeyBinding。 – Charlie 2009-07-29 21:24:49

1

对不起,我觉得这是实际的回答你的问题:

Button b = new Button(); 

b.Command = ApplicationCommands.Copy; 

List<string> gestures = new List<string>(); 

if (b.Command is RoutedCommand) 
{ 
    RoutedCommand command = (b.Command as RoutedCommand); 

    foreach (InputGesture gesture in command.InputGestures) 
    { 
     if (gesture is KeyGesture) 
     gestures.Add((gesture as KeyGesture).DisplayString); 
    } 
} 

如果你想要得到的原因是在显示它按钮的内容,你总是可以这样做:

<Button Command="ApplicationCommands.New" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"></Button> 

这将有按钮说“新”。