views:

333

answers:

2

I have an existing WinForms application for which I'm now designing new bits in WPF. Things are going reasonably well and I have run into my first need for a dialog.

I'd like to do the dialog in WPF. It appears as though I'm going to need to do a UserControl for the actual content and then host that content via a WinForms form with an ElementHost (since UserControl has no ShowDialog() method).

And that's where my question is. How does that work? Best I can tell, the WPF UserControl doesn't even have a DialogResult property (which makes sense given that it has no ShowDialog() method) - it looks to me like I'd need a WPF Window control - and I don't think I can use that in this case.

Struggling with the basic flow and setup of things here. Can someone shine a light?

Is this even possible?

A: 

If you want your interop application to work mostly like a WinForms app, then the approach you describe works fine -- I've pretty much the same thing in my interop cases.

WPF supports MessageBoxes (albeit a slightly different version than WinForms), and you could put something together using WPF Windows (extending it by adding something similar to DialogResult). However, the provided WPF controls suggest that they're trying to change UX interactions to minimize dialogs, particularly modal ones.

To make your life easier though, I would create a WinForms Form/ElementHost subclass specifically for dealing w/hosting WPF content, and depending on how clean you like your "using" declarations, wrapping your own DialogResult-like enumeration so you don't have to include the System.Windows.Forms namespace which can make your WPF code-behinds more cumbersome.

micahtan
But the approach that I described has a big missing piece - the DialogResult. Where does that come from? Am I able to somehow use the WPF Window control? If so, how?
Paul Prewett
DialogResult is a simple enum -- it's not hard to create a similar one (or re-use System.Windows.MessageBoxResult) and sub-class Window. However, since the core of your app is WinForms, I'd stick with having a specific Form-derived class created with an embedded ElementHost that you can use to wrap WPF content.
micahtan
+2  A: 

You can open a WPF window from a WinForms application.

Just create the window and call ShowDialog(). The CLR will load the WPF framework and open the window.

Cameron MacFarland
It appears as though this requires a separate project, however, to create the new WPF window, as that option simply doesn't appear in the default VS WinForms project template. It does work, however, once you do that. One other thing of note is that the ShowDialog() method from a WPF window returns ?bool instead of the more granular DialogResult that we're used to (which makes sense, of course - since it doesn't know anything about that class). Thanks, Cameron.
Paul Prewett