sgonchar
Topic Author
Posts: 21
Joined: 15 Mar 2021, 22:11

Creating focus manager / overwriting / attaching to focus system

08 Jun 2021, 00:20

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.

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2207
Joined: 22 Dec 2011, 19:20

Re: Creating focus manager / overwriting / attaching to focus system

10 Jun 2021, 02:08

Hi sgonchar,
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
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?
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?
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'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?
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 kinda just want to start with overwriting SetFocusAction::Invoke(), is that possible?
All the interactivity code is publicly available, so you can adapt it for your needs.
I've read elsewhere Keyboard.GotKeyboardFocus can be used, but can't find how to do it globally / in ViewModel.cpp.
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.
Do you have any pointers on debugging focus issues? Where to look for what element (by name) just asked for focus/got focus?
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.
<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>
Hope this helps, please let us know if you need assistance implementing any of these approaches.

Who is online

Users browsing this forum: No registered users and 2 guests