Click here to Skip to main content
14,970,143 members
Articles / Desktop Programming / WPF
Posted 23 May 2008


35 bookmarked

Integrate WPF UserControls in WinForms

Rate me:
Please Sign up or sign in to vote.
3.95/5 (15 votes)
23 May 2008CPOL1 min read
An article on embedding WPF- UserControls into WinForms
Image 1


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"
    Height="300" Width="300">

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



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:


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):


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)
        catch (Exception ex)

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


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


About the Author
Software Developer (Senior)
Germany Germany
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 which offers .NET-articles, code and news.

Comments and Discussions

SuggestionIn case this old article failed to help you understand Pin
Mr.TMG15-Dec-15 21:36
MemberMr.TMG15-Dec-15 21:36 
GeneralMy vote of 2 Pin
IAbstract18-Sep-11 8:09
MemberIAbstract18-Sep-11 8:09 
GeneralHelp Pin
Shashuma18-Aug-09 15:36
MemberShashuma18-Aug-09 15:36 
Generaldancing around the subject Pin
Kalor10-Mar-09 19:28
MemberKalor10-Mar-09 19:28 
GeneralRe: dancing around the subject Pin
IAbstract18-Sep-11 8:06
MemberIAbstract18-Sep-11 8:06 
GeneralFehler in C# Pin
dherrmann26-May-08 22:46
Memberdherrmann26-May-08 22:46 
GeneralRe: Fehler in C# Pin
codegod.de27-May-08 18:52
Membercodegod.de27-May-08 18:52 
QuestionPerformance Repercussions? Pin
Dominick O'Dierno23-May-08 14:20
MemberDominick O'Dierno23-May-08 14:20 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.