WPF 加速键,如 Visual Studio

发布于 2024-07-15 19:30:16 字数 293 浏览 4 评论 0原文

我正在使用 C# 和 WPF 开发一个大型业务应用程序。 我们的标准是在所有表单上的按钮上都包含加速键(保存、取消、搜索等)。 我最近注意到,加载表单时,即使用户没有按“Alt”键,加速键也会处于活动状态。 例如,我们的搜索按钮使用“Alt-H”作为加速键,但用户只需按“H”即可启动搜索。 我在按钮内容中使用标准“_”来创建加速键。

<Button Content="Searc_h"/>

有没有其他人注意到这种行为,并且有没有人找到需要按下“Alt”键的合适解决方法?

I'm developing a large line of business app with C# and WPF. Our standard is to include accelerator keys for buttons on all forms (Save, Cancel, Search, etc.). I've recently noticed that when a form is loaded, the accelerator key is active even when the user does not press the "Alt" key. For example, our Search button uses "Alt-H" as the accelerator, but the user can initiate a search by simply pressing "H". I'm using the standard "_" in the button content to create the accelerator key.

<Button Content="Searc_h"/>

Has anyone else noticed this behavior and has anyone found a suitable workaround that requires the "Alt" key to be pressed?

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

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

发布评论

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

评论(5

烟雨凡馨 2024-07-22 19:30:16

当没有聚焦文本输入时,这是 Windows 上加速键的标准行为。 请不要弄坏它,把它留在里面即可。

您可以自己检查一下。 按 Win+R 调出“运行...”对话框,然后按 Tab 键以聚焦其中一个按钮,然后按“B”。 “浏览”按钮将被激活。

这也是为什么您可以使用简单的 Y/N(而不是 Alt+Y / Alt+N)来回答这些 MessageBox 提示。

This is standard behaviour for accelerator keys on Windows whenever no text input is focused. Please don’t break it, just leave it in.

You can check this yourself. Press Win+R to bring up the "Run..." dialog, then Tab so one of the buttons is focused, then press "B". The "Browse" button will get activated.

This is also why you can answer those MessageBox prompts with a simple Y/N (instead of Alt+Y / Alt+N).

此岸叶落 2024-07-22 19:30:16

根本不要放入下划线,然后将其添加到窗口中。

  <Window.InputBindings>
    <KeyBinding Command="Help" Key="H" Modifiers="Alt"/>
  </Window.InputBindings>

Don't put the underscore in at all, then add this to the window.

  <Window.InputBindings>
    <KeyBinding Command="Help" Key="H" Modifiers="Alt"/>
  </Window.InputBindings>
墟烟 2024-07-22 19:30:16

在 WPF 中,无需按 Alt 键即可使用菜单和工具栏助记符。 这是微软的标准。
请参阅此链接 http://social .msdn.microsoft.com/Forums/en/wpf/thread/14f6f49f-0027-471b-b68c-e7f6ba012012

Menu and ToolBar mnemonics work without pressing Alt key in WPF. This is the microsoft standard.
Refer the this link http://social.msdn.microsoft.com/Forums/en/wpf/thread/14f6f49f-0027-471b-b68c-e7f6ba012012

坏尐絯℡ 2024-07-22 19:30:16

Sara Ford 的博客上的这篇文章讨论了这一现象。

评论者提到这是一个解决方案:

“如果您正在编写应用程序并且不喜欢这种行为,请查找 WM_CHANGEUISTATE,特别是 UISF_HIDEFOCUS 标志。”

This article on Sara Ford's Weblog discusses this phenomenon.

A commenter mentions this as a solution:

"If you're writing an application and you don't like this behavior, look up WM_CHANGEUISTATE and specifically the UISF_HIDEFOCUS flag."

残花月 2024-07-22 19:30:16

实际上,WPF 中的命令模式允许您更精细地控制允许使用哪些键盘快捷键。 它比按钮文本中的“_”更进一步。

检查以下链接以获取更多信息:

http:// www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

编辑:以前的链接已失效 - 提供了一个新链接。

-道格

Actually, the Command pattern in WPF allows you more granular control over what keyboard shortcuts are allowed. It goes a step further than the "_" in your button text.

Check the following link for more information:

http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

Edit: previous link was dead - provided a new link.

-Doug

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