Strange focus issue with UserControl hosted in ElementHost

I have a UserControl that is disabled in ElementHost

.
It contains DataGrid


When the user presses enter or double-clicks, a new line window (modalless) is displayed, and when this window is closed, the ElementHost window is activated back. The problem is the DataGrid is losing focus.

Snoop showed me that the visual tree has the following structure:

AdornerDecorator -> AvalonAdapter -> My UserControl.
Snoop shows that after activation the AdornerDecorator has properties

IsFocusable = false
IsFocused = false
IsKeyboardFocused = true
IsKeyboardFocusWithin = true

      

Which is absolute nonsense to me.
When the user presses Tab or arrow keys, the first activated UserControl receives focus.

I read this post and I am not happy with it:

Trick works differently for WPF and Windows Forms, and there were a few here we couldn't fix. If you have focus inside a WindowsFormsHost and either minimize / restore the form or show a modal dialog, the focus inside the WindowsFormsHost may be lost - the WindowsFormsHost still has focus, but the control inside it may not.

Now what? Relax and be happy?
And the frustration itself is the inability to reproduce the problem in other applications.

Can anyone explain what the reasons for this strange behavior are? Some workaround may be suggested.

+3


source to share


1 answer


This is a known bug: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7d41218e-c2bb-4957-b0b4-aa24efc4d8c2/

But unfortunately the link to the join is dead.



To work around this error, I had to add a property IsVmFocused

to my ViewModel, and also create my attached property FocusHelper.IsFocused

that listens for the element property change and the element's binding property IsFocused

.
You can see the xaml in this (asked) SO question: What is a cached user in a DataBinding context?

+2


source







All Articles