How to prevent keyboard focus from cycling?
Hi!
We are using Window class similar to the one in your app framework. And I'm trying to prevent the keyboard focus from cycling at its edges.
I've already tried KeyboardNavigation.DirectionalNavigation="Once" or even "None" and it doesn't seem do anything.
Overriding MoveFocus has no effect, the method seems to be bypassed.
Overriding OnPreviewLost(Got)KeyboardFocus and setting e.handled = true still allows the focus to be moved, albeit weirdly.
The only solution I've found so far is to set IsFocusEngagementEnabled=True on the whole window. This makes DirectionalNavigation="Once" work, but it messes up other things, so I'd like to avoid it if possible.
Are there other ways?
We are making customized keyboard/gamepad navigation, so I'm not averse to writing code to achieve it, but I don't see any place to plug my code in.
We are using Window class similar to the one in your app framework. And I'm trying to prevent the keyboard focus from cycling at its edges.
I've already tried KeyboardNavigation.DirectionalNavigation="Once" or even "None" and it doesn't seem do anything.
Overriding MoveFocus has no effect, the method seems to be bypassed.
Overriding OnPreviewLost(Got)KeyboardFocus and setting e.handled = true still allows the focus to be moved, albeit weirdly.
The only solution I've found so far is to set IsFocusEngagementEnabled=True on the whole window. This makes DirectionalNavigation="Once" work, but it messes up other things, so I'd like to avoid it if possible.
Are there other ways?
We are making customized keyboard/gamepad navigation, so I'm not averse to writing code to achieve it, but I don't see any place to plug my code in.
-
-
sfernandez
Site Admin
- Posts: 2865
- Joined:
Re: How to prevent keyboard focus from cycling?
Hi, have you tried to set the navigation mode to "Contained" in your Window? That mode indicates that focus won't get out of the Window and that it won't cycle.
Re: How to prevent keyboard focus from cycling?
"Contained" works. Thank you!
I didn't think about it because docs say "focus returns to the first or the last item when the end or the beginning of the container is reached", so I thought it would be the same as "Cycle".
Now I see that this confusion comes from Microsoft's documentation. Curiously, .NET reference sources describe "Contained" slightly better: "Like cycle but does not move past the beginning or end of the container.", but every other place I see has that confusing description.
I didn't think about it because docs say "focus returns to the first or the last item when the end or the beginning of the container is reached", so I thought it would be the same as "Cycle".
Now I see that this confusion comes from Microsoft's documentation. Curiously, .NET reference sources describe "Contained" slightly better: "Like cycle but does not move past the beginning or end of the container.", but every other place I see has that confusing description.
-
-
sfernandez
Site Admin
- Posts: 2865
- Joined:
Re: How to prevent keyboard focus from cycling?
Yeah, the documentation for Contained is a bit complex to understand. We will update it to make it clearer, like the one in .NET sources.
Thanks for your feedback!
Thanks for your feedback!
Who is online
Users browsing this forum: No registered users and 0 guests