Open a WPF Window from WinForms

If you find yourself in need to open a WPF Window from a WinForms program - this is one way to do it (works for me):

1) Create/Add a new project of type "WPF Custom Control Library"

image

2) Add a new Item of type "Window (WPF)"

image

3) Do your thing with the WPF Window

4) From your WinForms app, create and open the WPF Window:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();

There you go!

The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms. I understand there are other ways to do this and you can read more about WPF/WinForms interop here.

No Comments