I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
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
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
DotNetKicks.com
Mar 10, 2007 12:13 AM
#
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
#
Leave a Comment