tags:

views:

71

answers:

4
+2  Q: 

WPF and Winform

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?

A: 

I see no objection to do that.(I have in WinForms Application WPF windows) Many of the examples used MessageBox.Show which is part of the Windows.Forms. Of course you must rewrite all windows, not only controls.

Rafal T
so in a project is possible to have a Winform opening on a button, and a WPF one opening on a other button?
serhio
It becomes difficult if you have an MDI application, though, as the WPF Window does not expose a "MdiParent" property.
Robaticus
+2  A: 

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

hkon
+2  A: 

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).
Christian Hayter
A: 

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.)

mschaef