2014-09-24 86 views
0

我有一個WPF Ribbon Button,我希望它有一個快捷鍵。 (例如Ctrl + A)。在谷歌搜索,但失去了一個可能的答案。有誰知道我會如何處理這個問題?謝謝=)在WPF Ribbon Button中分配快捷鍵

這裏是我迄今爲止

<my:RibbonButton Name="rb1" Content="Images/save.png" /> 
<my:RibbonButton Name="rb2" Content="Images/abort.png" /> 

回答

1

你可以使用這種方法也許使用UIElement.InputBindingsKeyBinding

例如

<Window.InputBindings> 
    <KeyBinding Key="S" 
       Modifiers="Control" 
       Command="{Binding Save}" /> 
    <KeyBinding Key="A" 
       Modifiers="Control" 
       Command="{Binding Abort}" /> 
</Window.InputBindings> 

你可以輸入綁定對按鈕正在使用的命令的手勢

假定代碼

<my:RibbonButton Name="rb1" Content="Images/save.png" Command="{Binding Save}" /> 
<my:RibbonButton Name="rb2" Content="Images/abort.png" Command="{Binding Abort}" /> 

MSDN: KeyBinding

甲鍵綁定一個KeyGesture與ICommand的,如一個的RoutedCommand相關聯。 RoutedCommand是WPF命令系統的ICommand接口的主要實現。通常,在執行KeyGesture時,將調用該命令,但命令行爲進一步受命令特定因素(如CanExecute值)的影響。

+0

謝謝。但它不工作=( – 2014-09-24 06:44:10

+0

是否有任何錯誤?特別是數據綁定錯誤。你有命令嗎? – pushpraj 2014-09-24 06:58:22

+0

沒有錯誤顯示。只要我輸入「N」或「Ctrl + N」就沒有任何反應。也嘗試過「Alt + N」.. Yieh我確定命令與你發佈的地址相同。 – 2014-09-24 07:01:35

1

我更喜歡在代碼背後做它。我正在使用我的程序中的代碼。 希望它有幫助。

private void AddHotKeys() 
{ 
     try 
     { 
      RoutedCommand firstHotkey = new RoutedCommand(); 
      firstHotkey .InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Alt)); 
      CommandBindings.Add(new CommandBinding(firstHotkey , Save)); 


     } 
     catch (Exception err) 
     { 
      //handle exception error 
     } 

}