WindowsFormsHost is always the most top from WPF element
Asked Answered
G

4

12

how to set the z-index windowsformhost that they are not always at the top of the WPF element ?

Godman answered 29/3, 2012 at 6:55 Comment(2)
Simple answer: you can't. See here.Goosestep
Is there already a new solution how to solve this problem?Mathildemathis
I
15

According to MSDN (Layout Considerations for the WindowsFormsHost Element)

A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of WPF elements.

This is a design limitation

Another good article from MSDN that explains possible issues when using different graphical technologies in Windows is Technology Regions Overview

However googling I found that there seem to be some hackings for this (known as airspace restriction)

One hack (never tried it personally so not sure if it works) is at this link

Intrigue answered 29/3, 2012 at 9:10 Comment(1)
Web Archive has it: web.archive.org/web/20120728063220/http://khason.net/blog/…Verona
M
4

I've just encountered the same problem.

There is a potential workaround - depending upon the nature of the Windows Host window control and the WPF element you want to appear:

I bound the the WindowsFormsHost control's Visibility to a property on my view model to enable me to hide the host (and the controls on it) when I want to display the WPF that we want to appear over it.

Mantelletta answered 2/6, 2016 at 14:52 Comment(0)
P
3

Update, a few years later (2016-09):

My following answer, as noted by the top comment, is no longer valid, and was not available in the final version of .NET 4.5, or subsequent releases. Unfortunately, the link I included still has z-ordering information for HwndHosts present for the "current version" of .NET, which could lead some to believe this functionality does, in fact, exist. It doesn't. There is no work-around.

Original answer:

A year later, things have changed a bit with .NET 4.5. For those who stumbled upon this, much as I did, here is a more updated excerpt from Walkthrough: Arranging Windows Forms Controls in WPF on MSDN:

By default, visible WindowsFormsHost elements are always drawn on top of other WPF elements, and they are unaffected by z-order. To enable z-ordering, set the IsRedirected property of the WindowsFormsHost to true and the CompositionMode property to Full or OutputOnly.

All you need to do, when using .NET 4.5, is add the following attributes to your WindowsFormsHost element IsRedirected="True" and CompositionMode="Full" or CompositionMode="OutputOnly".

Pickel answered 8/4, 2013 at 21:29 Comment(3)
Checked it today - these properties were only in the beta release of .net 4.5. unfortunatelly they did't make their way to normal release.Fianna
1.3.10 Windows Presentation Foundation (WPF) 1.3.10.1 HwndHost feature has been removed from WPF in the .NET Framework 4.5 Beta The .NET Framework 4.5 Developer Preview included a WPF HwndHost redirection feature. However, this feature had several known issues and has been removed from the .NET Framework 4.5 Beta. It will not be included in any future releases. To resolve this issue: No workaround is available.Framing
How to use it in .NET 4? Is it possible?Whydah
C
-1

In my situation my WindowsFormsHost is in a two row Grid. The bottom row has a StackPanel in it that changes Height depending on what it contains. I handle that StackPanel's LayoutUpdated event to resize my WindowsFormsHost by subtracting it's ActualHeight from the Grid's ActualHeight. Be sure to use ActualHeight not Height.

     void ResizeWinhost()
    {
        mainGrid.UpdateLayout();
        detailPanel.UpdateLayout();
        winHost.Height = mainGrid.ActualHeight - detailPanel.ActualHeight - 5;
    }
Coyote answered 18/8, 2016 at 21:9 Comment(1)
How did your answer solve the problem of a WindowsFormsHost being always on top in a WPF application?Sissified

© 2022 - 2024 — McMap. All rights reserved.