Mehdi LAMRANI
Mehdi LAMRANI

Reputation: 11607

Add WPF Window in a Winforms Project in VS 2010

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.

Upvotes: 13

Views: 15835

Answers (5)

江晉緯
江晉緯

Reputation: 1

If you are using .Net

You can do this in the application properties

Right-click on the project > Properties > general > check "Windows Presentation Foundation"


Or edit the .csproj file

Add the following line

<UseWPF>true</UseWPF>

into

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    ...
  </PropertyGroup>
</Project>

For more: https://learn.microsoft.com/zh-tw/dotnet/core/project-sdk/msbuild-props-desktop

Upvotes: 0

Cenk
Cenk

Reputation: 91

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

Upvotes: 1

Carlos Borau
Carlos Borau

Reputation: 1473

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

Upvotes: 9

GrandMasterFlush
GrandMasterFlush

Reputation: 6409

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

Upvotes: 3

Tudor
Tudor

Reputation: 62469

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.

Upvotes: 8

Related Questions