Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / Windows Forms

WinForms / WPF Interop

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
17 Jun 2009CPOL3 min read 45.4K   409   14   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
    C#
    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:
    XML
    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:
    XML
    <!– 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
    C#
    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:
    C#
    // 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
QuestionProblem showing windowsFormsHost(windows Forms User Control) in WPF form Pin
Member 88126643-May-12 4:41
Member 88126643-May-12 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.