我有一个WPF控件,在某些情况下作为WPF应用程序的一个组件存在,另一个在Windows窗体中托管。如何检测第二种情况(WPF控件嵌入在WinForms中)?
答案 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...
}
}