Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a WPF Window from WinForms & link form app with WPF app

I'm using Visual studio 2012 & I'm using Win Form app called Form1 & add a new item which is wpf Window is called "wpfWin". I want to open the wpfWin through a button in the Form1 once it's clicked, it opened - separated windows-.

I have tried weblogs.asp but I haven't found the "WPF Custom Control Library" & once I skip it an error appeared. Is there any other way?!

Also, how can I link Two applications one in WinForm & the other Wpf ?!

like image 402
Sarah Avatar asked May 12 '13 19:05

Sarah


People also ask

Can you mix WPF and WinForms?

Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.

How do I open a WPF window?

Open as modal WPF restricts interaction to the modal window, and the code that opened the window pauses until the window closes. This mechanism provides an easy way for you to prompt the user with data and wait for their response. Once a window is closed, the same object instance can't be used to reopen the window.

How do I convert WinForms to WPF?

1) Target a certain part of your WinForms that you would like to switch up to WPF and then take it out. 2) Replace it with ElementHost. 3) Then in that ElementHost you will have your newly rewritten WPF counterpart. 4) Test it out make sure that it works okay with the rest of the elements.


1 Answers

Open WPF window in WindowsForm APP so you are having issues with the above linked tutorial and question? The "WPF Custom Control Library" is the WPF window that you want to use in your Win-Forms app.

Just a brief explanation as it is explained a few times through the question and tutorial. 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
 WindowsBase
 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.

like image 107
Nomad101 Avatar answered Sep 27 '22 16:09

Nomad101