1
votes

I have a WPF control which in some cases exists as a component of WPF application and in the other is hosted in Windows Forms. How can I detect the second case (a WPF control is embedded inside WinForms)?

1
I'd try Window.GetWindow(ctrl); and would expect it to return null, in a hosted environment.Rand Random

1 Answers

3
votes

Try this:

HwndSource wpfHandle = PresentationSource.FromVisual(this) as HwndSource;
if (wpfHandle != null)
{
    ElementHost host = System.Windows.Forms.Control.FromChildHandle(wpfHandle.Handle) as ElementHost;
    if(host != null)
    {
        //hosted in ElementHost...
    }
}