5

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

问候,贾瓦哈

4

4 回答 4

4

此代码适用于“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;
        }
    }
}
于 2015-07-17T18:57:01.297 回答
3

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

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

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

于 2010-07-22T07:49:03.797 回答
1

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

假设您的应用程序名为Heckler。将应用程序的命名空间标记添加到Window对象:

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

现在添加一个CommandBindings属性标签并开始您的CommandBinding对象集合。这里我们添加自定义命令Comment Selection

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

将 a 添加MenuItem到 mainMenuMenuItem

    <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.
}

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

于 2012-12-12T18:38:51.550 回答
-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();
}
于 2012-10-03T17:41:18.167 回答