6

我想为MenuItem. 我使用了下面的代码:

<MenuItem Header="_New" InputGestureText="CTRL+N" Click="NewMenu_Click">
    <MenuItem.Icon>
        <Image Source= "Images\NEW.PNG" Width="25" Height="28" />
    </MenuItem.Icon>
</MenuItem>`

但是InputGestureText当我按下CTRL+N. 我正在使用 Visual Studio Express Edition 2010。我在这里遗漏了什么吗?

4

3 回答 3

10

该属性的文档中非常明确:

此属性不将输入手势与菜单项关联;它只是将文本添加到菜单项。应用程序必须处理用户的输入以执行操作。有关如何将命令与菜单项关联的信息,请参阅 命令

于 2011-03-16T17:43:03.610 回答
4

最好的方法是创建一个Command, 并将InputGesture与该命令相关联:

public static class Commands
{
    public static readonly RoutedCommand CreateNew = new RoutedCommand("New", typeof(Commands));

    static Commands()
    {
        SomeCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control));
    }
}

...

// Wherever you want to create the MenuItem. "local" should be the namespace that
// you delcared "Commands" in.
<MenuItem Header="_New" Command="{x:Static local:Commands.CreateNew}">

...

// Wherever you want to process the command. I am assuming you want to do it in your 
// main window, but you can do it anywhere in the route between your main window and 
// the menu item.
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Commands.CreateNew}"> Executed="CreateNew_Executed" />
</Window.CommandBindings>

...

// In the code behind for your main window (or whichever file you put the above XAML in)
private void CreateNew(object sender, ExecutedRoutedEventArgs e)
{
    ...
}

如果您真的只想要一个“新建”命令,您可以跳过创建RoutedCommandand InputGesture,因为该命令已经为您创建:

<MenuItem Header="_New" Command="New">

...

<Window.CommandBindings>
    <CommandBinding Command="New" Executed="New_Executed" />
</Window.CommandBindings>

...

private void New_Executed(object sender, ExecutedRoutedEventArgs e)
{
    ...
}
于 2011-07-13T15:01:29.390 回答
3

一种不涉及命令和绑定的解决方案是覆盖拥有 Window 的方法并搜索具有与键盘事件匹配的OnKeyDowna 的菜单项。KeyGesture

这是 Window 的 OnKeyDown 覆盖的代码:

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);

    // here I suppose the window's menu is named "MainMenu"
    MainMenu.RaiseMenuItemClickOnKeyGesture(e);
}

这是将菜单项与键盘事件匹配的实用程序代码:

    public static void RaiseMenuItemClickOnKeyGesture(this ItemsControl control, KeyEventArgs args) => RaiseMenuItemClickOnKeyGesture(control, args, true);
    public static void RaiseMenuItemClickOnKeyGesture(this ItemsControl control, KeyEventArgs args, bool throwOnError)
    {
        if (args == null)
            throw new ArgumentNullException(nameof(args));

        if (control == null)
            return;

        var kgc = new KeyGestureConverter();
        foreach (var item in control.Items.OfType<MenuItem>())
        {
            if (!string.IsNullOrWhiteSpace(item.InputGestureText))
            {
                KeyGesture gesture = null;
                if (throwOnError)
                {
                    gesture = kgc.ConvertFrom(item.InputGestureText) as KeyGesture;
                }
                else
                {
                    try
                    {
                        gesture = kgc.ConvertFrom(item.InputGestureText) as KeyGesture;
                    }
                    catch
                    {
                    }
                }

                if (gesture != null && gesture.Matches(null, args))
                {
                    item.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));
                    args.Handled = true;
                    return;
                }
            }

            RaiseMenuItemClickOnKeyGesture(item, args, throwOnError);
            if (args.Handled)
                return;
        }
    }
于 2014-05-14T14:00:35.983 回答