2010-02-02 44 views
3

我有一個RoutedUICommandComment Selection。我需要爲這個命令添加一個輸入手勢,就像它在VIsual Studio中一樣,即。 (Ctrl + K,Ctrl + C)。 我該怎麼做? Plz幫助我。 (記住VS功能)。wpf中的多鍵手勢

問候,Jawahar

回答

2

我發現這個博客帖子,我認爲可能會幫

http://kent-boogaart.com/blog/multikeygesture

基本上,WPF沒有內置的支持它,但子類InputGesture或KeyGesture似乎是一個可能的方法來實現這一點,沒有太多的麻煩。

-1
<KeyBinding Command="{Binding ExitCommand}" 

       Key="{Binding ExitCommand.GestureKey}" 

       Modifiers="{Binding ExitCommand.GestureModifier}"/> 
get 

    { 

     if (exitCommand == null) 

     { 

      exitCommand = new DelegateCommand(Exit); 

      exitCommand.GestureKey = Key.X; 

      exitCommand.GestureModifier = ModifierKeys.Control; 

      exitCommand.MouseGesture = MouseAction.LeftDoubleClick; 

     } 

     return exitCommand; 

    } 

} 
private void Exit() 
{ 
    Application.Current.Shutdown(); 
} 
1

下面是我如何拼湊一些實際工作的東西。我只希望我可以信任爲我的啓蒙之路鋪平道路的人或人。

假設你的應用程序叫做Heckler。爲您的應用程序添加一個命名空間標記爲Window對象:

<Window ... 
    xmlns:w="clr-namespace:Heckler" 
    ...> 

現在添加CommandBindings屬性標記,開始你CommandBinding對象的集合。在這裏,我們添加自定義命令註釋選擇

<Window.CommandBindings> 
    <CommandBinding 
     Command="w:CustomCommands.CommentSelection" 
     CanExecute="CommentSelectionCanExecute" 
     Executed="CommentSelectionExecuted" /> 
</Window.CommandBindings> 

添加MenuItem到主MenuMenuItem

<Menu 
     IsMainMenu="True"> 
     <MenuItem 
      Header="_File"> 
      <MenuItem 
       Command="w:CustomCommands.CommentSelection"> 
      </MenuItem> 
     </MenuItem> 
    </Menu> 
    ... 
</Window> 

Window代碼隱藏,添加CustomCommands類和定製命令:

public static class CustomCommands 
{ 
    // Ctrl+Shift+C to avoid collision with Ctrl+C. 
    public static readonly RoutedUICommand CommentSelection = 
     new RoutedUICommand("_Comment Selection", 
      "CommentSelection", typeof(MainWindow), 
      new InputGestureCollection() 
      { new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) }); 
} 

現在線了您的事件處理程序:

private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
    // Determines status of command. 
    e.CanExecute = true; 
} 

private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e) 
{ 
    // TO-DO: Insert magic here. 
} 

你應該是好去。我希望這可以幫助,我沒有錯過任何東西!

3

此代碼爲 「Ctrl + W鍵,按Ctrl + E」 和/或 「按Ctrl + W,E」 組合製成,但您可以參數化它的任何組合鍵:

XAML:

<MenuItem Header="Header" InputGestureText="Ctrl+W, E" Command="ShowCommand"/> 

C#:

public static readonly RoutedUICommand ShowCommand = new RoutedUICommand(
    "Show command text", 
    "Show command desc", 
    typeof(ThisWindow), 
    new InputGestureCollection(new[] { new ShowCommandGesture (Key.E) })); 

public class ShowCommandGesture : InputGesture 
{ 
    private readonly Key _key; 
    private bool _gotFirstGesture; 
    private readonly InputGesture _ctrlWGesture = new KeyGesture(Key.W, ModifierKeys.Control); 

    public ShowCommandGesture(Key key) 
    { 
     _key = key; 
    } 

    public override bool Matches(object obj, InputEventArgs inputEventArgs) 
    { 
     KeyEventArgs keyArgs = inputEventArgs as KeyEventArgs; 
     if (keyArgs == null || keyArgs.IsRepeat) 
      return false; 

     if (_gotFirstGesture) 
     { 
      _gotFirstGesture = false; 

      if (keyArgs.Key == _key) 
      { 
       inputEventArgs.Handled = true; 
      } 

      return keyArgs.Key == _key; 
     } 
     else 
     { 
      _gotFirstGesture = _ctrlWGesture.Matches(null, inputEventArgs); 
      if (_gotFirstGesture) 
      { 
       inputEventArgs.Handled = true; 
      } 

      return false; 
     } 
    } 
}