Click here to Skip to main content
Click here to Skip to main content

Integrate WPF UserControls in WinForms

By , 23 May 2008
 

Introduction

In one of our last articles the author described how to embed WinForms-Controls into WPF. This one explains the other way around: how can one integrate a WPF-UserControl into WinForms. In the example, a scaleable WPF-image is hosted in a WinForms-Dialog.

The Concept

For hosting a WPF-Usercontrol in WinForms you can use the control ElementHost that resides in the namespace System.Windows.Forms.Integration. This control has a property called Child to which you can assign an instance of an UIElement.

The Project

Here is the XAML for our ScaleableImageControl. It is very simple, just an image with a BitmapEffect:

<UserControl x:Class="ScaleableImageControl.ScaleableImageCtrl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

        <Image Margin="10" x:Name="img" Stretch="Uniform" Opacity="1">
            <Image.BitmapEffect>
                <DropShadowBitmapEffect Opacity="1" />

            </Image.BitmapEffect>
        </Image>

</UserControl>

Now, we add two methods to the Usercontrol that we will call from the Windows Forms dialog:

public void SetSource(string fileName)
{
    img.Source = new BitmapImage(new Uri(fileName));
}

public void SetOpacity(double opacity)
{
    img.Opacity = opacity;
}

We are able to set the opacity and the displayed image with this interface.

Hosting the WPF-control in WinForms

When opening the Winforms dialog in the designer of VS 2008 you will realize that there is a ScaleableImageControl in the toolbox:

WPF_Winforms_2.jpg

You can drag it into your Winforms dialog. An ElementHost-instance and a ScaleableImageControl-instance is created for you, the ScaleableImageControl-object is assigned to the Child-property of the ElementHost-object. VS 2008 helps us a lot here. At last we need some controls to interact we the ScaleableImageControl-object (assigning images and setting the opacity):

WPF_Winforms_3.jpg

Here is the code for interaction:

private void udOpacity_ValueChanged(object sender, EventArgs e)
{
    scaleableImageCtrl1.SetOpacity( (double) udOpacity.Value);
}

private void btnBrowse_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            scaleableImageCtrl1.SetSource(openFileDialog1.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

It is not a big deal to integrate WPF in Winforms. Try it, the project is attached. Have fun!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

codegod.de
Software Developer (Senior)
Germany Germany
Member
I studied informatics at Johannes-Gutenberg University in Mainz (Germany). I'm a developer for 12 years and involved in .NET-Projects since the first .NET beta.
 
My homepage is www.codegod.de which offers .NET-articles, code and news.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 2memberIAbstract18 Sep '11 - 8:09 
lacking in some very important beginner tips.
GeneralHelpmemberShashuma18 Aug '09 - 15:36 
Hi codegod.de
Great Article. I was just having headache of adding WPF usercontrols in WinForms.
 
I'm developing a image viewer application using C# WinForms at the moment. I'm trying to have a flash smooth interface for the viewer. That's when I started looking into WPF. In your source code, you have a seperate folder called "ScaleableImageControl". How did you create this folder?Did you just add a new WPF project to the WinForm project? How did you create the ScaleableImageControl.dll file?
 
Thanks
Generaldancing around the subjectmemberKalor10 Mar '09 - 19:28 
What a godawful article. The reader only wants to know one thing (how to get a usercontrol into winforms) and you DON'T SAY HOW. What, it just magically appears in my toolbox, does it??Mad | :mad:
GeneralRe: dancing around the subjectmemberIAbstract18 Sep '11 - 8:06 
Kind of dated ...but, yeah, I was wondering the same thing. It seems there are quite a few questions left unanswered that seem to have been irresponsibly overlooked (as Shashuma asks the following):
 
How did you create this folder?
Did you just add a new WPF project to the WinForm project?
How did you create the ScaleableImageControl.dll file?

The author has succeeded in demonstrating the ease of interaction with a WPF control once it is hosted; however, I think the article does fail in providing some very necessary pointers.
GeneralFehler in C#memberdherrmann26 May '08 - 22:46 
Hallo,
habe deine demo heruntergeladen und gestartet.
bekomme aber leider den fehler:
Warnung 2 Die Komponente "ScaleableImageControl", auf die verwiesen wird, wurde nicht gefunden.
 
was ist zu tun?
 
grüße-
dietrich
GeneralRe: Fehler in C#membercodegod.de27 May '08 - 18:52 
Hi,
 
you have to open the solution in VS 2008 and build it. Works fine for me.
 
Visit my page www.codegod.de

QuestionPerformance Repercussions?memberDominick O'Dierno23 May '08 - 14:20 
Is there any performance impact to this? how much slower would a control with lots of animation be than within a standard WPF app?
 
Free the mallocs!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 23 May 2008
Article Copyright 2008 by codegod.de
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid