简体   繁体   English

创建 WPF 应用程序时,如何防止菜单栏被选项卡聚焦

[英]When creating a WPF application, how do I prevent a menu bar from being focused with tab

I'm trying to define a menu bar in XAML.我正在尝试在 XAML 中定义一个菜单栏。 My menubar looks like this:我的菜单栏如下所示:

<DockPanel Margin="1,1,-1,-1">
   <Menu DockPanel.Dock="Top" >
      <MenuItem Header="_File">
      <MenuItem Header="_Settings" />
      <MenuItem Header="_Exit" />
      </MenuItem>
      <MenuItem Header="_Aircraft">
         <MenuItem Header="_A2A Fuel and Payload Manager..." />
      </MenuItem>
   </Menu>
</DockPanel>

When I run my application, pressing Tab will set focus on the menubar, in addition to the other controls in the application.当我运行我的应用程序时,除了应用程序中的其他控件之外,按 Tab 键会将焦点设置在菜单栏上。 Standard Windows behavior is not to tab onto the menubar, but just to press Alt to access it from the keyboard.标准 Windows 行为不是在菜单栏上按标签,而只是按 Alt 从键盘访问它。 Can anyone explain why the default in WPF is to put the menubar in the tab order and how I can remove it?谁能解释为什么 WPF 中的默认设置是将菜单栏置于标签顺序中以及如何将其删除?

Every control in wpf has TabIndex, so to skip some controls it's enough to set KeyboardNavigation.TabNavigation="None" wpf 中的每个控件都有 TabIndex,因此要跳过一些控件,只需设置KeyboardNavigation.TabNavigation="None"

If you want to exclude the whole menu bar from keyboard navigation, use the attached property KeyboardNavigation.TabNavigation .如果要从键盘导航中排除整个菜单栏,请使用附加属性KeyboardNavigation.TabNavigation Setting None will disable tab navigation for all children of Menu .设置None将禁用Menu所有子项的选项卡导航。

<DockPanel Margin="1,1,-1,-1">
   <Menu DockPanel.Dock="Top" KeyboardNavigation.TabNavigation="None">
      <!-- Your menu items -->
   </Menu>
</DockPanel>

The reason for the MenuItem s having keyboard focus in the first place is that the default value for the attached property is Continue , which essentially means: If children are a tab stop, they will receive focus. MenuItem具有键盘焦点的原因首先是附加属性的默认值为Continue ,这实质上意味着:如果子项是制表位,他们将获得焦点。

If you only want to exclude specific controls from keyboard navigation, set IsTabStop="False" .如果您只想从键盘导航中排除特定控件,请设置IsTabStop="False" This will only affect the control itself, not its children.这只会影响控件本身,而不影响其子项。

<MenuItem Header="_File" IsTabStop="False">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何防止按钮被箭头聚焦? - C# How do I prevent buttons from being focused by arrows? 选中后如何防止显示标签? - How do I prevent a tab from rendering when selected? 如何防止在我运行 WPF MVVM 应用程序时引发“System.StackOverFlowException”异常? - How to prevent the "System.StackOverFlowException" exception being thrown when i run my WPF MVVM application? 防止WPF文本框在聚焦时更改外观 - Prevent WPF Textbox from changing appearance when focused WPF / XAML:如何执行线程处理并防止主UI忙/死? - WPF / XAML: How do I execute threaded processes and prevent the main UI from being busy / freezing? 如何防止 WPF 按钮在单击后保持突出显示? - How do I prevent WPF buttons from remaining highlighted after being clicked? 在创建单元测试时,如何防止方法被调用? - What can I do to prevent the method from being called when creating Unit Test? 在WPF应用程序中按下Return键时如何模拟Tab键? - How do I simulate a Tab key press when Return is pressed in a WPF application? 如何防止 ObservableCollection 在 WPF 中无故刷新? - How can I prevent the ObservableCollection from being refreshed for no reason in WPF? 如何防止C#控制台应用程序中的Ctrl键生成特殊字符? - How do I prevent special characters from being generated by the Ctrl key in a C# console application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM