Posted by The Tallest on 06/24/2008

7 comments
Skill

WPF Tutorial - Using WPF In WinForms

Posted in:

A little while back, we did a tutorial on how to embed WinFoms controls inside of WPF application (you can read all about it here). Today, we are going to flip that on its head - we are going to take a look at how to embed WPF controls inside of a WinForms application.

You might be thinking to yourself "why would someone want to do that?" A great question, and there are a couple different reasons. First, perhaps you have an application already written in WinForms, but you want to move to WPF. Instead of rewriting the entire thing from scratch (which might be a large amount of work, and might be time/cost prohibitive), you can rewrite portions of the application as time allows or when you need to redo a particular section anyway.

Another reason would be if there is a area of your app that would be easy to do in WPF (maybe because of the animation features, or the composibility) while in WinForms it would be difficult.

So what do we need to do to enable this integration? Well, first off, we need to add a reference to the Visual Studio project. You do this by right clicking on "References" in the Solution Explorer and choosing "Add Reference":

Add Reference Context Menu

Once you have the dialog up, you want to scroll to the bottom on the .NET tab and choose the "WindowsFormsIntegration" component, and hit OK:

Add Reference Dialog

At this point, you probably want to create the WPF user control that you want to add to your app. If its already written, you can add it through the Add Existing Item menu option, and if you need to write one, you can choose "Add New Item" and under WPF choose "User Control". In my case I want to create a new one, and I'm going to name it "MyWPFControl":

Add New Item Dialog

So my made-up reason why I want a WPF control in my WinForms app is that I want a custom combo box - or really a combo box with some custom items. While this is doable in WinForms, it is hard. Downright annoying in many ways. In WPF, though, its really easy, since you can put whatever you want in a ComboBoxItem. In this case, my user control consists of a combo box with three items - a circle, a triangle, and a rectangle:

<UserControl x:Class="WPFInWinForms.MyWPFControl"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Height="300" Width="300">
  <Grid>
    <ComboBox>
      <ComboBoxItem Margin="2">
        <Ellipse Width="20" Height="20" Stroke="Black" />
      </ComboBoxItem>
      <ComboBoxItem Margin="2">
        <Path Stroke="Black" Data="M 0,0 L 20,0 L 10,20 Z" />
      </ComboBoxItem>
      <ComboBoxItem Margin="2">
        <Rectangle Stroke="Black" Width="20" Height="20" />
      </ComboBoxItem>
    </ComboBox>
  </Grid>
</UserControl>

Ok, now that we have the control we want to add, how do we add it? Well, there are two ways of going about this - one is through the designer, and the other is by adding the control manually in code. We are fist going to take a look at the designer way of doing things.

When we added that reference earlier, one of the things it did was give us access to a control called ElementHost. This control is for WPF in WinForms what the WindowsFormsHost was for WinForms in WPF. In the designer, you can find this control in the Toolbox underneath "WPF Interoperability":

WPF Interoperability Toolbox Section

So once you drag an ElementHost onto your form, the designer makes things really easy for you. You just click on the little arrow on the upper right of the ElementHost and you get a couple options. The one we care about is the combobox for "Select Hosted Content". When you click this, you get to choose what WPF control you want to host in the ElementHost:

ElementHost options

Once you do that, well, thats it. You can run the app, and the WPF control will show up exactly where you put it:

WPF in WinForms

Doing all of this in code is also quite easy. In fact, I'll just throw all the code up here in one block:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WPFInWinForms
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();

      ElementHost elhost = new ElementHost();
      elhost.Size = new Size(110, 60);
      elhost.Location = new Point(45,35);

      MyWPFControl wpfctl = new MyWPFControl();
      elhost.Child = wpfctl;

      this.Controls.Add(elhost);
    }
  }
}

So the first thing needed is that last using statement. The ElementHost lives in that namespace, so we add a using statement for it. After that, we just create a new ElementHost, and set its normal WinForms properties. Then we create an instance of the wpf control we want, and add that as the child for the ElementHost. Finally, we add the ElementHost as a child to the current form - and we are all done!

So that is it for using WPF controls inside of Windows Forms. You can download the little sample app from here if you would like to play with the code yourself. As always, if you have any questions or comments, feel free to leave them below.

Santosh
07/01/2008 - 04:50

thankx for such a simple to follow tutorial.

I have one question, how should we use the WPF user control which is created in control library?
The control library will have multiple xaml file, do we have to add all those manually? can we just add reference to control library DLL and add it under element host?

reply

The Tallest
07/01/2008 - 08:42

Yup, you should be able to just add a reference to the dll and you'll have access to any of the controls in that dll.

reply

Jeremy
08/24/2008 - 14:53

Can I embed a wpf control in a dotnet 2.0 winform ?

reply

Jeremy
08/24/2008 - 14:55

... how would embed manually just using the sdk?

reply

AmsaKANNA
09/06/2008 - 07:36

i've coded my project in vb .net 2005(windows application)... Now i'ld like to do the same project in vb .net 2008(wpf application) along with expression blend... i'm new to *.xaml.vb files... properties and codes of *.vb differs from *.xaml.vb... (like me.hide and me.show are missing in *.xaml.vb and msgbox() is replaced by messagebox.show())... so, can u pls list me out *.xaml.vb codes or guide me for the same... thanks in advance

reply

Rutvij
11/02/2008 - 23:24

your tutorial is very nice. It's completely step by step tutorial that helps beginners.......

What if I want to add text in each combo box items i.e. in first item u have added ellipse - I want text beside it as "Ellipse".... how this is possible ?

reply

Anonymous
06/05/2009 - 18:45

You could add a stack panel in the Combobox item and add elements in the stack panel.

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.