もっと詳しく

I recently ran into a solution for one of the commonly known “Airspace issues” of the WindowsFormsHost. As a result of my research i figured out, that WindowsFormsHost (and any other windows forms content) is always rendered on top of WPF content.
My problem was that a WPF window shown above a WindowsFormsHost could not be brought back to foreground when the WindowsFormsHost control once gained focus again.

To reproduce the issue create a WPF Window and put a WindowsFormsHost into it. Put a Windows Forms Control in the host. Create a Button that opens a new WPF Window.

Then run the application. Click the button and the new WPF Window will appear. Fine.
But now activate the main window again. The main window will be in foreground and the second window will be in the background. If you try to bring the second window back into the foreground either by using Alt+Tab or by selecting it from the taskbar – in my case it never appeared, or at most flickered shortly. When digging deeper into the code if found out, that the Activated event of the second window is raised correctly, but without any chance to prevent to immediatly following Deactivated event.

I figured out by testing, that when any WPF content of the main window has the focus when deactivating the window, the issue will not occur. So i fixed the whole thing by registering the Deactivated event of the main window and calling Focus() for any kind of WPF control (e.g. the button). If the second window is brought back to the foreground, the main window will still have the focus on the WPF content and the WindowsFormsHost will not try to get itself on top.

I don’t really KNOW why this Airspace thing appears, i am also not sure if the solution is a generic one.
At least it solved the problem of bringing a WPF window back in foreground of a WindowsFormsHost.

Attached below you find some links on the same topic and some general introduction to the Airspace topic.

http://windowsclient.net/learn/integration.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx

http://stackoverflow.com/questions/5978917/render-wpf-control-on-top-of-windowsformshost

http://blogs.msdn.com/b/jaimer/archive/2007/03/07/using-visualtreehelper-getdrawing-to-get-around-the-z-order-limitation-when-hosting-windows-forms-controls.aspx

Hope this helps!