Windows Forms is the the technology for developing client applications on the windows platform and has been in existance for quite some time. WPF (Windows Presentation Foundation) is the new technology for developing apps with Vista look and feel. These two technologies are all different from each other and there needs to be some interoperability between these two technologies. What I mean by this is, the Winforms Apps that are developed till now need to function together with WPF apps and vice versa.
Crossbow Project is the solution for this. Using this, a WPF control can be hosted inside a winforms project and a winforms control can be hosted within WPF App. The assembly that supports this is WindowsFormsIntegration.dll and is shipped with .net framework 3.0.
In this blog, I will show simple examples of how to host a WPF control in a Winforms app and vice versa.
Hosting a WPF Control in a Winforms App
We use the class called ElementHost (System.Windows.Forms.Integration.ElementHost) which is a winforms control to host a WPF control say (System.Windows.Controls.Button).
To do this, we do the following steps
1. Create a Windows Forms Application in C#
2. Add reference to WindowsFormsIntegration.dll (ElementHost Control)
3. Add reference to WindowsBase.dll, PresentationCore.dll, PresentationFramework.dll (WPF button System.Windows.Controls.Button is in presentationframework.dll, that has dependency on WindowsBase.dll, PresentationCore.dll).
4. Add the following code to Form1() Contructor after InitializeComponent()
//Creating a new WPF Button
System.Windows.Controls.Button wpfButton = new System.Windows.Controls.Button();
//Setting the Content
wpfButton.Content = “My WPF Button”;
//Create a new ElementHost
ElementHost eh = new ElementHost();
//Set EH’s child to WPF Button
eh.Child = wpfButton;
eh.Location = new System.Drawing.Point(100, 100);
//Add the EH Control to the form
Hosting a WindowsForms Control in WPF App
We use the class called WindowsFormsHost (System.Windows.Forms.Integration.WindowsFormsHost) which is a WPF control to host a Winforms Control say (System.Windows.Forms.Button).
To this, we do the following steps
1. Create a WPF Application in C#
2. Add reference to WindowsFormsIntegratin.dll (ElementHost Control)
3. Add reference to System.Windows.Forms.dll (System.Windows.Forms.Button control)
4. In Window1.xaml, add “Loaded” Attribute to the <Window> tag and set the “Name” attribute to the <Grid> tag shown below
Title=”Window1″ Height=”300″ Width=”300″
Add the Loaded attribute to the Window tag will add an event “Window_Loaded” in the Window.xaml.cs file
5. Add the following code the Window_Loaded event
// Create a new WindowsFormsHost object
System.Windows.Forms.Integration.WindowsFormsHost wfh = new System.Windows.Forms.Integration.WindowsFormsHost();
//Create a Windows Forms Button and set its content
System.Windows.Forms.Button wfButton = new System.Windows.Forms.Button();
wfButton.Text = “My Windows Forms Button”;
//Set the child of windowsformshost object to the windowsforms button
wfh.Child = wfButton;
//Add the windowsformshost control to the wpf window
In this way, WPF and Winforms Apps can interact with each other.