2016-11-10 60 views
-3

一个WinForms窗口在XML文件:如何显示在一个WPF网格或Viewbox控件

<Viewbox name="viewbox1"></Viewbox> 

在C#代码隐藏:

windowformClass window1 = new windowformClass(); // this is the WinForms object, not WPF 
viewbox1.Child = window1; 

当我指定窗口1至viewbox1它显示了一个错误,不能将窗体窗体转换为System.Window.ElementUI

什么是正确的方法来做到这一点?

+1

您需要使用WindowsFormsHost这个任务 – 0x4f3759df

+0

你可以给我任何例如对于此> –

+0

你应该避免在'WPF'应用程序中使用'WindowsForms' - 如果需要使用'WindowsFormsHost'正如其他人所提到的。为什么不把'WinForm'重构成'WPF'? –

回答

1

对于名为WindowsFormsHost的WinForms,您应该使用WPF的interop主机控件。下面
的代码萃取,并适于从MSDN

// Create the interop host control. 
System.Windows.Forms.Integration.WindowsFormsHost host = 
    new System.Windows.Forms.Integration.WindowsFormsHost(); 

// Create your control. 
windowformClass window1 = new windowformClass(); 

// Assign the control as the host control's child. 
host.Child = window1; 

// Set the interop host control as the ViewBox child. 
viewbox1.Child = host; 

A user reported a problem with hosting the WindowsFormHost inside a ViewBox。如果您遇到问题,您应该检查一下。

因为你(显然)是一个新来者,我想给你一个小建议: 有关MSDN和SO这个主题上的大量资源可以通过简单的搜索找到你最喜欢的搜索发动机。
下次在发布问题前尝试找到解决方案,以便仅保留主题的最佳答案,并将版主保存为重复提问的工作。
更多关于的是,检查help from SO

相关问题