如何向菜单项添加访问键?

发布于 2024-08-18 09:26:39 字数 232 浏览 5 评论 0原文

<MenuItem x:Name="newProjectButton" Click="OnNewProjectButton_Click" Header="_New Project">
</MenuItem>

我想在按下 Alt+N 时调用 OnNewProjectButton_Click 。不幸的是,上面的代码不起作用,因为只有在菜单展开(即具有焦点)时才会调用处理程序。

<MenuItem x:Name="newProjectButton" Click="OnNewProjectButton_Click" Header="_New Project">
</MenuItem>

I want to call OnNewProjectButton_Click whenever Alt+N is pressed. The code above doesn't work, unfortunately, because the handler is called only if the menu is expanded (i.e. has focus).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹淡然 2024-08-25 09:26:39

您可以使用 ApplicationCommands.New为此,因为它已经提供了该功能。默认的 WPF 命令模型非常酷。即使您决定不使用默认的命令模型,第二个链接也应该向您展示如何连接所需的输入手势。

编辑:这是一个示例实现...

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New" 
                    CanExecute="NewApplicationCommand_CanExecute"
                    Executed="NewApplicationCommand_Executed" />
</Window.CommandBindings>

<Grid>

    <Menu>
        <MenuItem Header="_File">
            <MenuItem Command="ApplicationCommands.New" Header="_New Project"  />
        </MenuItem>
    </Menu>

</Grid>

以及背后的代码...

    private void NewApplicationCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // Whatever logic you use to determine whether or not your
        // command is enabled.  I'm setting it to true for now so 
        // the command will always be enabled.
        e.CanExecute = true;
    }

    private void NewApplicationCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Console.WriteLine("New command executed");
    }

You could use ApplicationCommands.New for this since it already provides that functionality. The default WPF Command Model is pretty cool. Even if you decide not to use the default commanding model, that second link should show you how to hook up the input gestures you need.

EDIT: Here is a sample implementation...

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New" 
                    CanExecute="NewApplicationCommand_CanExecute"
                    Executed="NewApplicationCommand_Executed" />
</Window.CommandBindings>

<Grid>

    <Menu>
        <MenuItem Header="_File">
            <MenuItem Command="ApplicationCommands.New" Header="_New Project"  />
        </MenuItem>
    </Menu>

</Grid>

And the code behind...

    private void NewApplicationCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        // Whatever logic you use to determine whether or not your
        // command is enabled.  I'm setting it to true for now so 
        // the command will always be enabled.
        e.CanExecute = true;
    }

    private void NewApplicationCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        Console.WriteLine("New command executed");
    }
夏天碎花小短裙 2024-08-25 09:26:39

您会看到设置 InputGestureText 但与WinForms

<MenuItem Header="Paste" 
 ToolTip="Paste the selected text to text box" 
 InputGestureText="Ctrl+V" />

不同的是,"应用程序必须处理用户的输入执行该操作”

因此,请考虑使用 WPF 命令,因为它们会自动为您执行此操作。我发现 Windows Presentation Foundation Unleashed 很好地涵盖了这一点。

You see to set the InputGestureText on the menu item

<MenuItem Header="Paste" 
 ToolTip="Paste the selected text to text box" 
 InputGestureText="Ctrl+V" />

But unlike WinForms the "The application must handle the user's input to carry out the action".

So consider using the WPF commands as they do this for you automatically. I found that Windows Presentation Foundation Unleashed covers this well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文