【问题标题】:multi key gesture in wpfwpf中的多键手势
【发布时间】:2011-01-12 00:36:02
【问题描述】:

我有一个名为Comment SelectionRoutedUICommand。我需要为这个命令添加一个输入手势,就像它在 VIsual Studio 中一样,即。 (Ctrl+KCtrl+C)。 我怎样才能做到这一点?请帮助我。 (牢记 VS 功能)。

问候,贾瓦哈尔

【问题讨论】:

    标签: wpf hotkeys routed-commands


    【解决方案1】:

    此代码适用于“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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我发现了这篇我认为可能会有所帮助的博文

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

      基本上,WPF 没有对其的内置支持,但子类化 InputGesture 或 KeyGesture 似乎是一种可能的方式来实现这一点,而不会有太多麻烦。

      【讨论】:

        【解决方案3】:

        这是我如何拼凑出一些真正有效的东西。我只是希望我能相信那些为我的启蒙之路铺平道路的人。

        假设您的应用程序名为 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.
        }
        

        你应该很高兴。我希望这会有所帮助,我没有错过任何东西!

        【讨论】:

          【解决方案4】:
          <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();
          }
          

          【讨论】:

            猜你喜欢
            • 2010-10-16
            • 1970-01-01
            • 2016-04-20
            • 2020-08-17
            • 1970-01-01
            • 1970-01-01
            • 2011-04-05
            • 2013-12-03
            • 1970-01-01
            相关资源
            最近更新 更多