2009-07-29 35 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> 

這將有按鈕說「新」。