سؤال

I have UserControl which is hoseted in ElementHost.
It contains DataGrid
When user presses enter or double clicks the row new window is shown (modalless), and when that window is closed window with ElementHost is activated back. The problem is that DataGrid loses focus.

Snoop showed me that visual tree has the following structure:

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

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

What is an absolute nonsense to me.
When user hits Tab or arrow keys the first enabled UserControl's element receives focus.

I've read this post and I'm not happy with it:

Focus works differently for WPF and Windows Forms, and there were some rough edges around here that we were unable to 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 very disapointing is inability to reproduce the problem in other applications.

Can anyone explain what are the reasons of such a strange behavior? May be offer some workaround.

هل كانت مفيدة؟

المحلول

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

But unfortuantely the reference to connect is dead.

To workaround this bug I had to add IsVmFocused property to my ViewModel and also create my FocusHelper.IsFocused attached property which listens for element's IsFocused property change and bound VM's property.
You can see the xaml in this (task related) SO question: What is a Cached acessor in the context of DataBinding?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top