Wednesday 18 May 2016

Open a WPF Window from WinForms

You will want to open the win-forms project you are currently working on. Then you will want to go to Solution Explorer and right-click the SOLUTION, not the project, go to "Add" and select "New Project".
In the add new Project go to Visual C# and then Windows in the tree on the left hand side. They should be located in the installed sub menu. In the main section you should see "WPF Custom Control Library" click on it and then name it what you would like and click ok.
Add a Window(WPF) control to the project, this window would be the WPF window that you want to open.
Then from the WinForm, open it like so:
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
However ensure you have the following using statements:
using System; //Given 
using System.Windows.Forms; //Given
using System.Windows.Forms.Integration; //Not so Given.
You will need to add some references as well to make this work correctly, here is the list which should be all you need to add to a win-forms:
 PresentationCore
 PresentationFramework
 WindowsFormsIntegration
 System.Xaml
 YourWpfControlProjectName
You should add these to your Win-forms project using the reference picker in VS by right-clicking the reference folder in the solution explorer and adding a new reference. All of the references are located in the Framework tab, sans your WPF control which is in the solution tab.