0
votes

I added a WPF project to my solution which contained one project - a Winforms one. Then I referenced the WPF project from the Winforms one and added the appropriate references. I then created a new WPF Window in the original Winforms project and opened it from within a button event handler. It worked fine.

HOWEVER,

in the past when searching for a similar solution I don't remember seeing this solution mentioned (of simply adding another project to the solution) so I was wondering: Are there pitfalls when adding a WPF Window to a Winforms app?

EDIT

I'm not referring to embedding it inside a Form. Just a separate Window (perhaps instead of the main Form). And I'm also not referring to challenges that are apparent such as the need to translate Bitmaps to their WPF equivalents. I'm only referring to pitfalls that are not noticed, where the app compiles and seems to run, but, for example, later when being executed on a machine with a different DPI - shows things incorrectly.

1
The WinForms Project becomes DpiAware (SystemAware). If it was not already DpiAware, that is. Except that, not much. Unless when you say adding a WPF Window you mean embed it. If that's the case, I don't know, I've never done it.Jimi
@Jimi Thanks. (And I do not mean embed. That might create more problems.)ispiro
That comment is of course in relation to simple coexistence. Cooperation/interaction may need special treatment, depending on the roles. DataBinding works differently, ObservableCollection, while it can be used in WinForms, doesn't have the same support from the Framework, BindingList and BindingSource are commonly used instead, so you may need a proxy to enable two-way databindings, if required. Basic graphics objects (Image, Bitmap vs. BitmapSource, BitmapImage, WriteableBitmap), though easily convertible, are not directly compatible etc.Jimi
In relation to DataBindings, objects that implement INotifyPropertyChanged are compatible and fully functional in both Frameworks. The BindingSource class works very well with this kind of objects, when a DataTable is not available.Jimi

1 Answers

2
votes

The only obvious pitfall is that the WPF window won't get any keyboard input unless you call ElementHost.EnableModelessKeyboard before you open it in the Windows Forms application:

Window1 window = new Window1();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);
window.Show();