Keyvan Nayyeri

My daily musings about software and technology

Host WPF Controls in Windows Forms

Previously I discussed about Hosting Windows Forms Controls in Windows Presentation Foundation.  This is a more common scenario but in opposite direction you may want to host WPF controls in Windows Forms applications.  This is easy, too and I'll talk about it in this post.

Like hosting a Windows Forms control in WPF where you could put a WindowsFormsHost element to host your Windows Forms control inside it, you must put an ElementHost control on your Windows Form to host your WPF control.

On the other hand you must add four references to PresentationCore, PresentationFramework, WindowsBase and WindowsFormsIntegration assemblies.  You can remember the last one for hosting Windows Forms controls in WPF.

Like developing for WPF you can create your WPF controls and you have access to their properties and methods but you need to create an ElementHost control to keep these WPF controls on Windows Form and show them.  By adding this ElementHost control to your Windows Form you'll be able to get benefits of WPF controls in your Windows Forms applications.

As an example I create a simple Windows Forms application that hosts a WPF TextBox control with a default text and shows a MessageBox whenever user changes the text in this TextBox.

To do this I add required references to my solution and code.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Windows.Forms.Integration;

using System.Windows.Controls;

 

namespace HostWPFInWinForms

{

    public partial class frmMain : Form

    {

My Windows Form has a Panel control named containerPanel which is added to keep my ElementHost control.  Now in form initialization I add my main code to create a WPF TextBox on fly and set some properties and event handlers for it.  Then create an ElementHost object and add my TextBox as its Child property.  At the end I add this ElementHost control to my Panel to show it on my Windows Form.

public partial class frmMain : Form

{

    public frmMain()

    {

        InitializeComponent();

 

        System.Windows.Controls.TextBox wpfTextBox =

            new System.Windows.Controls.TextBox();

        wpfTextBox.Name = "myTextBox";

        wpfTextBox.Text = "WPF TextBox";

        wpfTextBox.TextChanged +=

            new TextChangedEventHandler(textbox_TextChanged);

 

        ElementHost elementHost = new ElementHost();

        elementHost.Dock = DockStyle.None;

        elementHost.Width = 150;

        elementHost.Height = 50;

        elementHost.Child = wpfTextBox;

 

        containerPanel.Controls.Add(elementHost);

    }

 

    void textbox_TextChanged(object sender, TextChangedEventArgs e)

    {

        MessageBox.Show("Text Changed!", ":-D");

    }

}

Now if I run this application, a Windows Presentation Foundation TextBox appears on my Windows Form.  If I change the text in TextBox then a MessageBox will be shown to inform me about a change in text.

Now playing: Pink Floyd - Learning To Fly

14 Comments

Kareem Ayoub
Apr 06, 2007 12:39 AM
#
Excellent article ! this really helped me alot ! Thank you.

Kareem Ayoub
Apr 06, 2007 12:44 AM
#
Is there a way to set that wpftextbox MultiLine property to true ??

Eric Stump
Apr 20, 2007 7:31 PM
#
Is there any way to add a WPF control to a windows form using the raw XAML instead of building in code?

haris
May 18, 2007 12:36 AM
#
i need your help my friend you make the textbotton in wpf Subtract , ADD and Multiply . must make this textbotton in .netfermwork ples help me i am waiting your anser

Ahmed Mahmood
May 18, 2007 12:48 AM
#
Hello Dear i hav an assigmnet in which i make a page on .netframe work with using WPF technology.Moreover the page contains 3 buttons like ADD,SUbstartc and multiply and on bottom 1 more exit button.... Kindly help me out as i have zero knowledge abt WPF.... But Reply Me asap REgards AHmed

Kareem Ayoub
May 22, 2007 5:37 PM
#
Ahmed, Contact me if you need help about WPF controls. Kareem@DevEgypt.com

Cabello
Aug 25, 2009 10:18 AM
#

Thanks a lot, man! :) I need to integrate WPF in my WinForms and this seems to be the easiest/fastest way! :)


G.Karthikeyan
Jun 08, 2010 5:40 AM
#
Hats Off.
Continue ur Great Work.

eco
Jul 12, 2010 3:18 AM
#
thanks... great article

Lucio Paoli
Sep 01, 2010 5:37 AM
#
you did not add the textbox to the elementhost, again you miss to add reference to UIAutomationTypes, si you're handling a standard textbox control

Maryam
Sep 26, 2010 10:43 PM
#
kheily aalie man taze webloge shomaro didam

kervin
Oct 06, 2010 3:19 AM
#
Is it possible to integrate a whole WPF Usercontrol in a windows form?

manit
Apr 28, 2011 12:29 PM
#
thanks a lot! your code has helped me lot..

no_namee
Jul 11, 2011 6:24 AM
#
Hi,
Hi,
I have to use a WindowsForm element in WPF application.I used for this purpose "WindowsFormHost" element.It works but,at runtime compiler gives me this exception ;

"Set property 'System.Windows.Forms.Integration.WindowsFormsHost.Child' threw an exception.'" although I set it;

myFormHost = new WindowsFormsHost();
myForm = new Form();
myFormHost.Child=myForm;
I will approciate if you help me,it is so important..Thanks in advance for your reply.

Leave a Comment