Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to host a WinForm form within a WPF Form via a container/wrapper?

Is there a way to host/display a full WinForms form (not just a single control) within some sort of container or wrapper type control within a WPF form? I’m looking for something similar in concept to a virtual include from php or iframe in html. Possibly by compiling it into an OCX or DLL.

like image 734
jasonk Avatar asked Feb 19 '10 00:02

jasonk


2 Answers

you can do it with the following code:

In your XAML:

<WindowsFormsHost name="wfHost" />

In your code-behind:

Form foo = new Form();
foo.TopLevel = false;
foo.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
wfHost.Child = foo;
like image 54
whoisthemachine Avatar answered Oct 05 '22 05:10

whoisthemachine


To my knowledge you can't host an actual WinForms Form in WPF.

However a good alternative is to create a normal UserControl in WinForms (as a Windows Forms Control Library project) that contains all required functionality and child controls.

Then in your WPF project, reference the WindowsFormsIntegration.dll (should be in the .NET tab of Add References).

Then also reference the assembly containing the WinForms UserControl and finally add a WindowsFormsHost container to the XAML.

For more information see this useful tutorial by Sacha Barber.

like image 38
Ash Avatar answered Oct 05 '22 05:10

Ash