如何检查WPF控件是否托管在WinForms中?

时间:2017-09-27 11:34:26

标签: c# wpf winforms winforms-interop

我有一个WPF控件,在某些情况下作为WPF应用程序的一个组件存在,另一个在Windows窗体中托管。如何检测第二种情况(WPF控件嵌入在WinForms中)?

1 个答案:

答案 0 :(得分:1)

试试这个:

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...
    }
}