Open a WPF Window from WinForms & link form app with WPF app
Asked Answered
V

4

5

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 ?!

Virgil answered 12/5, 2013 at 19:49 Comment(2)
So you want two entirely separate windows? or a WPF control on a Win-forms Window?Catto
separate windows, EditedVirgil
C
22

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.

Catto answered 12/5, 2013 at 19:59 Comment(10)
let's say that I names the "WPF Custom Control Library" Pan & I add a new wpf window in Pan called wind ; So the code would be > var wpfwindow = new Pan.wind(); ?!Virgil
Yes exactly that. if you have issues with something coming up as undefined check your references.Catto
it doesn't work, should I add the wpf references to the winApp?!Virgil
yes add, at the very least, the ones I have listed and you should be good.Catto
"YourWpfControlProjectName" you mean the cs file !? default name CustomControl1 ?!Virgil
No the name of the project, it should be in the add references window under the solution tab if you followed the tut.Catto
It works correctly ! Thank you so much indeed Nomad101 :) & Sorry for asking a lot. It's my first time :)Virgil
I also had to add WindowBase to reference as well.Amen
I discovered I can make the WPF Window temporarily into a "WPF Custom Control Library" project then copy it into an existing standard Class library project. Where interestingly it already allowed me to make WinForm as well as WPF User Controls. The assembly compiles fine and I can launch either LogWindowWF (a WinForm) or LogWindowWPF from a WPF application. This is useful if you wanted to keep other related classes into one assembly.Undervest
I wasted 4 hours of my life because I didn't include the EnableModelessKeyboardInterop line and couldn't figure out why I wasn't able to navigate with arrow keys. For anyone reading, this line is extremely important... don't leave it out!Valleau
O
1

It's also very important to check the .NET Framework version of your projects. Your referenced WPF application must have the same or lower version than your root project. took me a lot of time!

Outside answered 19/4, 2020 at 9:2 Comment(0)
B
0

Try referencing windowbase.dll in your project. Also ensure that the "Copy Local" property is set to true.

Bonanno answered 10/7, 2015 at 20:56 Comment(0)
P
0
  • No need to create dedicated .csproj

  • Ensure to support wpf in csproj definition by having both properties:

    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
    
  • Add a new "Wpf Window":

add wpf

  • If you already added it an still have issue, ensure the constructor is correctly defined and calls

    InitializeComponent();

Pox answered 25/6, 2024 at 14:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.