Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WinForms / WPF Interop

0.00/5 (No votes)
17 Jun 2009 1  
What you need to do to successfully interop Winforms with a WPF application and vice versa

In this blog entry, I will discuss what you need to do to successfully interop Winforms with a WPF application and vice versa.

In order to evaluate these 2 different interop methods, 2 demo projects have been created which carry out the functionality shown below:

  • .NET Winforms userControl within a .NET3.0/3.5 WPF application
  • .NET3.0/3.5 WPF userControl within a .NET Winforms application

A UserControl was chosen for its ease of development coupled with the fact that the Interop item will always be hosted in a Window of the interop hosts choosing.

As such, the interop test item had to either be a UserControl or CustomControl. UserControl was picked for the reason stated above.

By using these 2 demos project the .NET 2.0 <-> .NET3.0/3.5 Interop is able to be tested for suitability for use on a commercial product.

Just What Is Interop

Interoperability is a property referring to the ability of diverse systems and organizations to work together (inter-operate). The term is often used in a technical systems engineering sense, or alternatively in a broad sense, taking into account social, political, and organizational factors that impact system to system performance.

With respect to software, the term interoperability is used to describe the capability of different programs to exchange data via a common set of exchange formats, to read and write the same file formats, and to use the same protocols.

http://en.wikipedia.org/wiki/Interoperability, up on date 26/02/2008

How to Get a Winform Control Working Within a WPF Application

In order use a Winforms UserControl/CustomControl within a WPF application host, the following steps must be taken:

  1. Reference the WindowsFormsIntegration.dll (should be in the GAC)
  2. Reference the actual Winforms UserControl/CustomControl projects
  3. Add the following using statement to the code behind for the Window/Page that is hosting the Winforms UserControl/CustomControl
    using System.Windows.Forms.Integration;
  4. Within the Window/Page that is hosting the Winforms UserControl/CustomControl, add an xmlns declaration which declares the actual Winforms UserControl/CustomControl DLL, an example is as follows:
    xmlns:winforms="clr-namespace:WinformsUserControl;
    assembly=WinformsUserControl"
  5. Within the Window/Page that is hosting the Winforms UserControl/CustomControl, add a Winforms interop container to host the actual UserControl/CustomControl, and also add and name the UserControl/CustomControl. An example is as follows:
    <!– Winforms User Control >
    <WindowsFormsHost  Height="auto" Width="auto">
        <winforms:WinformsUserControl x:Name="winformsUserControl"
    
    
    
    CustomEvent="WinformsUserControl_CustomEvent" />
    </WindowsFormsHost>

I tested out the following features, and this is what the results were:

FeatureDid It Work
Displayed the same as Winforms versionNo
Property setYes
Property getYes
Method callYes
Custom Event SubscriptionYes
Custom EventArgsYes

How to Get a WPF Control Working within a Winforms Application

In order use a WPF UserControl/CustomControl within a Winforms application host, the following steps must be taken:

  1. Reference the WindowsFormsIntegration.dll (should be in the GAC)
  2. Reference the actual WPF UserControl/CustomControl projects
  3. Add the following using statement to the code behind for the Form that is hosting the WPF UserControl/CustomControl
    using System.Windows.Forms.Integration;
  4. Within the Window/Page that is hosting the WPF UserControl/CustomControl, add a WPF interop container to host the actual UserControl/CustomControl, and also add and name the UserControl/CustomControl, and example is as follows:
    // Create the Instance Fields
    private System.Windows.Forms.Integration.ElementHost elhostUserControl11;
    private WPFUserControl.WPFUserControl wpfUserControl;
    
    // Within InitializeComponent
    this.elhostUserControl11 = new System.Windows.Forms.Integration.ElementHost();
    this.wpfUserControl = new WPFUserControl.WPFUserControl();
    ....
    ....
    
    this.elhostUserControl11.Location = new System.Drawing.Point(14, 54);
    this.elhostUserControl11.Name = "elhostUserControl11″;
    this.elhostUserControl11.Size = new System.Drawing.Size(270, 27);
    this.elhostUserControl11.TabIndex = 0;
    this.elhostUserControl11.Text = "elementHost1″;
    this.elhostUserControl11.Child = this.wpfUserControl;
    ....
    ....
    
    this.Controls.Add(this.elhostUserControl11);

I tested out the following features, and this is what the results were:

FeatureDid It Work
Displayed the same as WPF versionYes
Normal Property setYes
Normal Property getYes
Dependency Property setYes
Dependency Property getYes
Method callYes
Pre-Built Routed Event subscriptionYes
Custom Routed event subscriptionYes

To illustrate this further, please find attached a small demo solution.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here