Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a project containing both Winforms and WPF?

Tags:

winforms

wpf

Is it possible to have a project containing both Winforms and WPF?

Say a WinForm project that is transformed step by step(form by form) in a WPF one, will be possible to have a Winform opening on a button, and a WPF one opening on a other button?

like image 215
serhio Avatar asked Jun 30 '10 13:06

serhio


4 Answers

Adding Winforms to WPF projects can be done smoothly (directly from the "Add new item" menu), but there is not straight option to add a WPF window to a Winforms project. Still, I handled to do it following these steps:

  1. Add a WPF User Control (this option is available on the "Add new item" menu) and then convert it into a WPF Window. Modify the XAML changing the UserControl parent tag to Window, and remove the inheritance from UserControl (all of this is explained in this link).

  2. Add a reference to System.Xaml.dll. See this link.

  3. Add a reference to System.Windows.dll (I found it on my computer on this path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5. Be aware it might be different in yours). See this link.

like image 193
Gedeon Avatar answered Nov 19 '22 03:11

Gedeon


Yes. You have to pick one technology to display each physical window and control in your app, but there's no reason why you can't mix and match.

For example:

  • A WinForms window can show a WPF window.
  • A WPF window can show a WinForms window.
  • A WinForms window can contain WPF content (see the ElementHost control).
  • A WPF window can contain WinForms controls (see the WindowsFormsHost control).
like image 26
Christian Hayter Avatar answered Nov 19 '22 05:11

Christian Hayter


This works great. One can have WPF windows in Windows Forms and Windows Forms windows in WPF

http://msdn.microsoft.com/en-us/library/ms745781.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx

like image 36
hkon Avatar answered Nov 19 '22 04:11

hkon


What you might be looking for is the ElementHost control. What it lets you do is take WPF content and host it in a Windows Forms window. More details are here:

http://msdn.microsoft.com/en-us/library/ms745781.aspx

There is also a control that lets you do the reverse: host Windows Forms content from within WPF:

http://nayyeri.net/host-windows-forms-controls-in-wpf

Between the two, you can move the 'dividing line' between WPF and Windows Forms with some degree of flexibility.

There is at one caveat you'll need to keep in mind. Windows Forms works internally in terms of HWND's... a window managed by the legacy Windows window manager (which handles the z-order). WPF doesn't do this... A WPF tree is typically rendered into a single HWND', and it's WPF that manages things like z-order. What this means to you is that z-order doesn't always work the way you expect it to, and there are things you can't do with hosted Windows Forms controls that you can do with traditional WPF elements. (There is actually a way to solve this, but it involves periodically rendering the HWND into a memory bitmap, rendering that bitmap into a WPF surface, and then redirecting events directed to the WPF surface to the underlying HWND. This is powerful, but tricky and difficult to get right.)

like image 1
mschaef Avatar answered Nov 19 '22 05:11

mschaef