2016-11-10 62 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

相關問題