Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the keyboard navigation work correctly in the Navigatioin controls. #181

Merged

Conversation

walterlv
Copy link
Contributor

@walterlv walterlv commented May 11, 2022

What does this PR do

Before this PR, the up key and the down key do not work when the Navigation view gets focused. And after this, you can press up and down to navigate focus among the navigation items, just like what the Windows Settings and the Microsoft Store do.

  • Up/Down: Focus changes among the navigation items from the first to the last. The change stops when the first or the last has arrived.
  • Tab/Shift+Tab: Focus will enter the navigation view only once and the next Tab will change the focus to other controls. If you want to switch focus inside it, use the Up/Down key instead.
  • Ctrl+Tab: Not defined. It will behave in a default way which is changing the focus to the next and next until outside of the navigation view. If the user of the WPFUI package wants to switch it like a TabControl, he/she has to set the KeyboardNavigation.ControlTabNavigation attached property to Once manually.

What changed in this PR

  1. OnKeyDown is overridden in the NavigationItem class. we handle the up and down keys to navigate focus states. If the navigation happens, the Handled property is set to true to prevent further event handling. (Reason: If we do not set the Handled to true, the ScrollViewer will handle it so that the KeyboardNavigation will not do further navigation. That is why we need to do the navigation ourselves but not using the native behavior that the KeyboardNavigation provides.)
  2. OnKeyDown is overridden in the Navigation class. In most cases, this method does nothing because it does not receive focus by default. But if someone sets focus on it, the key handling can move the focus to its navigation children.
  3. We override some metadata of KeyboardNavigation to set the default values of the navigation behavior. We set the directional navigation to Contained and the tab navigation to Once to make sure that the focus behavior of our Navigation works just like what the Windows Settings and the Microsoft Store do. We make the control-tab navigation to keep its default value because this behavior is hard to determine in a class library.

More information on the KeyboardNavigationMode enum can be found here:

@pomianowski pomianowski self-assigned this May 11, 2022
@pomianowski pomianowski added the controls Changes to the appearance or logic of custom controls. label May 11, 2022
@pomianowski pomianowski changed the base branch from main to development May 11, 2022 18:48
@pomianowski pomianowski merged commit ac5ba87 into lepoco:development May 11, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
controls Changes to the appearance or logic of custom controls.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants