I'm Keyvan Nayyeri, a 28 years old software engineer working at Match.Com and living in Dallas, Texas.
I have a Master’s degree in computer science and a bachelor's degree in applied mathematics. I’m also known to be a technical author with several technical publications in the form of books and articles. Besides, I'm an open source enthusiast and have coordinated or contributed to several projects. Currently, I maintain my projects on GitHub.
As a content provider on the internet, not only I publish on this technical blog, but also I'm a podcaster and publish audio podcasts on Mash This.
Trying to maintain a healthy and active lifestyle, I'm a pescetarianist and exercise almost everyday. I’m an avid runner, soccer defender, and tennis player. I also have an interest in fashion.
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
Kareem Ayoub
Apr 06, 2007 12:39 AM
#
Kareem Ayoub
Apr 06, 2007 12:44 AM
#
Eric Stump
Apr 20, 2007 7:31 PM
#
haris
May 18, 2007 12:36 AM
#
Ahmed Mahmood
May 18, 2007 12:48 AM
#
Kareem Ayoub
May 22, 2007 5:37 PM
#
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
#
Continue ur Great Work.
eco
Jul 12, 2010 3:18 AM
#
Lucio Paoli
Sep 01, 2010 5:37 AM
#
Maryam
Sep 26, 2010 10:43 PM
#
kervin
Oct 06, 2010 3:19 AM
#
manit
Apr 28, 2011 12:29 PM
#
no_namee
Jul 11, 2011 6:24 AM
#
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