13

Is it possible to access all the WPF Items from a winforms Project when selecting "add new Item" in VS 2010 ? I only have access to WPF userControl by default.

I would like to add a WPF Window to a winforms project. Not just a user control.

EDIT : Short answer : This does not seem to be possible per se, but it is possible to add WPF resources and the necessary references manually.

Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130

4 Answers4

8

Apparently you cannot directly, but what you can do is add a new user control and then modify the code to make it a Window. Simply create a new WPF project, add a window and see what you need to change to turn your user control into a window.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I thought of doing that but I expected it not to be very straightforward and a bit hair-pulling, so I did not even try :-) I'll give it a shot – Mehdi LAMRANI Dec 16 '11 at 12:37
  • Well the first steps would be to replace the UserControl tag with the Window tag in the XAML and make the class extend Window. Then see what else. – Tudor Dec 16 '11 at 12:39
  • This is probably the best solution, and explained here step by step: http://www.i-think22.net/archives/2008/08/05/adding-wpf-windows-to-an-existing-windows-form-project/ – surfen Dec 17 '11 at 15:28
7

In my opinion the "cleanest" option is using this scheme:

  1. Create a WPF project (add any WPF windows you need). Lets call it "WPFProject"
  2. In the same solution create a WinForms project (add any Forms you need). Lets call it "MainProject".
  3. In MainProject add references to:

    • WPFProject
    • PresentationCore
    • PresentationFramework

Thats all, now you can open your WPF windows from your MainProject (eg. by pressing a button):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim mywpfform = New WPFProject.MainWindow //MainWindow is the default name of your first WPF window. Obviously you can open any other
        mywpfform .Show()
End Sub
Carlos Borau
  • 1,433
  • 1
  • 24
  • 34
3

It is possible to do this, I've had to do use this technique a few times without any problems:

How to programmatically create a WPF window in a WinForm application

Community
  • 1
  • 1
GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
0

It is possible to use WinForms and WPF in same project. A WinForm project can be modified so that WPF windows and other WPF elements can be added. For this you have to add WPF project GUID to the WinForms project’s manifest file.

Open project manifest file (one with the .csproj extension) and modify PropertyGroup node (the one without Condition element) as below:

  <PropertyGroup>
     <ProjectTypeGuids>
         {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
     </ProjectTypeGuids>
  </PropertyGroup>

After this modification restart VS. Now you can add WPF window or other WPF elements. You may also have to add references System.Xaml, PresentationCore and PresentationFramework.

For more please check: https://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs and http://www.mobilemotion.eu/?p=1537

Cenk
  • 71
  • 1
  • 3