[英]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.