Crossbow - Interoperability between Windows Forms and WPF

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
this.Controls.Add(eh);

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
<Window x:Class="WpfApplication1.Window1"
xmlns="
https://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x="
https://schemas.microsoft.com/winfx/2006/xaml "
Title="Window1" Height="300" Width="300"
Loaded="Window_Loaded">
<Grid Name="MyGrid">
</Grid>
</Window>

 
 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
this.MyGrid.Children.Add(wfh);

In this way, WPF and Winforms Apps can interact with each other.

Comments

  • Anonymous
    April 08, 2008
    Hello Naga, I am experiencing a problem with hosting WPF content in a Winform.  I'm using 4 ElementHosts placed on top of an invisible WinForm.  It works fine on my development machine, however, when I run it on another machine, with .NET 3.0 installed, a problem occurs. The problem is that the top elementhost, that has drag capability, drags off what looks like the elementhost, and leaves the WPF XAML content fixed in place.   On my development machine, this top piece drags the underlying Winform that the other elementHosts, and vis-i-vis, WPF content is docked to, and they all move with the mouse. Any ideas? What I am missing? Any help is appreciated. Thanks Victor.    

  • Anonymous
    April 08, 2008
    The comment has been removed