Creating focus manager / overwriting / attaching to focus system
Hello,
We've had several focus related bugs / investigations.
Latest: something is stealing focus from an element that we called setFocusAction on, we can't find what ... or setFocusAction just doesn't run, I can't figure out why.
I'm wondering how can we in ViewModel.cpp plug into the focusing system, detect focus changes, overwrite/prevent things from focusing or manually focus on an element?
I've read elsewhere FocusManager tends to be implemented by gameteams .. this is probably what we're aiming for, I'm wondering if there's an example?
I kinda just want to start with overwriting SetFocusAction::Invoke(), is that possible?
I've read elsewhere Keyboard.GotKeyboardFocus can be used, but can't find how to do it globally / in ViewModel.cpp.
Do you have any pointers on debugging focus issues? Where to look for what element (by name) just asked for focus/got focus?
Cheers.
We've had several focus related bugs / investigations.
Latest: something is stealing focus from an element that we called setFocusAction on, we can't find what ... or setFocusAction just doesn't run, I can't figure out why.
I'm wondering how can we in ViewModel.cpp plug into the focusing system, detect focus changes, overwrite/prevent things from focusing or manually focus on an element?
I've read elsewhere FocusManager tends to be implemented by gameteams .. this is probably what we're aiming for, I'm wondering if there's an example?
I kinda just want to start with overwriting SetFocusAction::Invoke(), is that possible?
I've read elsewhere Keyboard.GotKeyboardFocus can be used, but can't find how to do it globally / in ViewModel.cpp.
Do you have any pointers on debugging focus issues? Where to look for what element (by name) just asked for focus/got focus?
Cheers.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Creating focus manager / overwriting / attaching to focus system
Hi sgonchar,
Hope this helps, please let us know if you need assistance implementing any of these approaches.
When SetFocusAction is not working is usually because the target of the focus cannot be focused: is the element enabled? focusable? is it part of the UI tree, i.e., has Loaded being called on the element?something is stealing focus from an element that we called setFocusAction on, we can't find what ... or setFocusAction just doesn't run, I can't figure out why
The ViewModel doesn't have access to the UI elements, thus, the focus. The focus is easier to track in the code-behind of your root control, by just managing the PreviewGotKeyboardFocus event. There you have all the information about who previously had the focus and who will get it next.I'm wondering how can we in ViewModel.cpp plug into the focusing system, detect focus changes, overwrite/prevent things from focusing or manually focus on an element?
There are no examples of that feature. We have plans to officially add support for this (#1982), but take into account that this feature is something more like a logical focus, which is not exactly the keyboard focus (there can be different logical focus scopes, but there is only 1 keyboard focus).I've read elsewhere FocusManager tends to be implemented by gameteams .. this is probably what we're aiming for, I'm wondering if there's an example?
All the interactivity code is publicly available, so you can adapt it for your needs.I kinda just want to start with overwriting SetFocusAction::Invoke(), is that possible?
As I said before, this is not something that is better done in the code-behind of controls, not in the ViewModels. From a Control you can just call GetKeyboard()->GetFocused() to get the focused element.I've read elsewhere Keyboard.GotKeyboardFocus can be used, but can't find how to do it globally / in ViewModel.cpp.
Maybe what you need is just a new behavior that you can attach to the root of your application, which will hook to the PreviewGotKeyboardFocus event to update a FocusedElement property. Then you can use that property in the xaml to debug the focus owner.Do you have any pointers on debugging focus issues? Where to look for what element (by name) just asked for focus/got focus?
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:local="clr-namespace:Testing">
<i:Interaction.Behaviors>
<local:KeyboardFocusTrackerBehavior x:Name="focusTracker"/>
</i:Interaction.Behaviors>
<TextBlock Text="{Binding FocusedElement, ElementName=focusTracker}"/>
...
</Grid>
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 14 guests