Skip to content

Why is WindowsFormsHost or WebBrowser content annoying me?

Jan Karger edited this page May 2, 2019 · 1 revision
  • Why is so-and-so WinForms control inside the WindowsFormsHost invisible or not rendering?
  • Why is the WebBrowser (or other control) covering my Flyout (or another control)? [AIRSPACE]

What you are facing is an issue colloquially called the Airspace issue. Due to the different underlying graphical engines used in WinForms (GDI) and WPF (DirectX and such), there are some interop issues. Sometimes, your controls appear will be invisible. Sometimes, your WinForms control may overlap your WPF controls.

It is important to know that this is an WPF issue, not a MahApps.Metro one.

By default, the vanilla Window class and MahApps.Metro's MetroWindow has AllowsTransparency set to false. If you set this in MahApps.Metro's MetroWindow initialized event to true you will get this issue.

Solution

The solution, should for some reason that you need AllowsTransparency to be set to true or get the rendering issue with WindowsFormsHost is:

  • Go to https://microsoftdwayneneed.codeplex.com/ or now on GitHub here or here
  • Get (compile) the lib and add it as a reference to your project.
  • Add xmlns:interop="clr-namespace:Microsoft.DwayneNeed.Interop;assembly=Microsoft.DwayneNeed" to your XAML.
  • Place your Control in the AirspaceDecorator:
<interop:AirspaceDecorator AirspaceMode="Redirect"
                           Background="White"
                           IsInputRedirectionEnabled="True"
                           IsOutputRedirectionEnabled="True">
    <!--your winforms control or webbrowser here here-->
</interop:AirspaceDecorator>