简体   繁体   中英

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. 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. Standard Windows behavior is not to tab onto the menubar, but just to press Alt to access it from the keyboard. Can anyone explain why the default in WPF is to put the menubar in the tab order and how I can remove it?

Every control in wpf has TabIndex, so to skip some controls it's enough to set KeyboardNavigation.TabNavigation="None"

If you want to exclude the whole menu bar from keyboard navigation, use the attached property KeyboardNavigation.TabNavigation . Setting None will disable tab navigation for all children of 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.

If you only want to exclude specific controls from keyboard navigation, set IsTabStop="False" . This will only affect the control itself, not its children.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM