by Keyvan Nayyeri via Keyvan Nayyeri on 3/10/2007 7:10:02 AM
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 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
Original Post: Host WPF Controls in Windows Forms
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.