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

Sarah
  • 75
  • 1
  • 1
  • 7

3 Answers3

21

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.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
Nomad101
  • 1,688
  • 11
  • 16
  • 1
    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(); ?! – Sarah May 12 '13 at 20:28
  • 1
    Yes exactly that. if you have issues with something coming up as undefined check your references. – Nomad101 May 12 '13 at 20:35
  • 1
    it doesn't work, should I add the wpf references to the winApp?! – Sarah May 12 '13 at 20:42
  • 1
    yes add, at the very least, the ones I have listed and you should be good. – Nomad101 May 12 '13 at 20:43
  • 1
    "YourWpfControlProjectName" you mean the cs file !? default name CustomControl1 ?! – Sarah May 12 '13 at 21:19
  • 1
    No the name of the project, it should be in the add references window under the solution tab if you followed the tut. – Nomad101 May 12 '13 at 21:22
  • It works correctly ! Thank you so much indeed Nomad101 :) & Sorry for asking a lot. It's my first time :) – Sarah May 12 '13 at 21:36
  • 1
    I also had to add WindowBase to reference as well. – TNA May 07 '14 at 05:48
  • 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. – Meryan Feb 20 '17 at 07:20
  • 1
    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! – Mash Sep 18 '18 at 23:11
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!

F.Sun
  • 11
  • 1
0

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

SoftwareCarpenter
  • 3,835
  • 3
  • 25
  • 37
PinoyDev
  • 949
  • 1
  • 10
  • 21