Winforms / WPF Interop

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, and 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

Feature Did It Work
Displayed the same as Winforms version No
Property set Yes
Property get Yes
Method call Yes
Custom Event Subscription Yes
Custom EventArgs Yes

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

Feature Did It Work
Displayed the same as WPF version Yes
Normal Property set Yes
Normal Property get Yes
Dependency Property set Yes
Dependency Property get Yes
Method call Yes
Pre-Built Routed Event subscription Yes
Custom Routed event subscription Yes

To illustrate this further please find attached a small demo solution, which is available here

About these ads

14 thoughts on “Winforms / WPF Interop

  1. Kashif says:

    Nice topic. I am able to get this interop working with .NET 3.5 WinForms/WPF pages. My question is how can I add WPF page to .NET 2.0 WinForm. Which will help me not to ask our client to do major framework push. any other suggestions would be helpful. Thanks,

  2. sacha says:

    I dont think you can actually do that. I think you can only host WPF Elements is Winforms. I have not tried Page, but I suspect it doesnt inherit from Control, so cant be hosted in a Windows Forms ElementHost.

    Sorry, never tried, so cant give 100% truthful answer, but that would be my suspicion.

  3. Kim says:

    Really thank you for your great introduction.

    I am very serious in implementing smooth coloring in WPF like this “http://www.bobpowell.net/pgb.htm”
    Would you please let me know how can I implement in WPF? Is it possible?
    Any idea?

    Thank you so much.

    - Kim

  4. Kim says:

    Oh.. I see.
    Is it impossible just to provide a direction to smooth coloring and possibility.. so that I can try it by myself.
    I appreciate your help. :)

    - Kim

    PS) BTW, How much do you charge for this task?

  5. Mmmm Kim, its not what I meant, what I really meant that time is scarse, and I do not have much, I write blogs/articles, but do not really have time for much else.

    Sorry.

  6. Peter says:

    Hi there,

    First of all thanks for the article. I was able to figure out the WFP -> WinForm interop.

    I can use my WinForm Control in a WPF form now. But my goal is to embed the control in a WPF Web Browser app.

    Everything works fine until I launch the App. I get an exception just by using the element.

    The exception is “That assembly does not allow partially trusted callers.” I don’t get it!, this happens even before adding the ref to my WinForm control. And as I said before it worked for a WPF windows app.

    I know it is an issue with the “AllowPartiallyTrustedCallersAttribute” but my hands are tied since my assembly is not in the picture yet.

    If you read this and have something to say, I’ll appreciate it.

    Thanks!

    PKatz

  7. sacha says:

    Yeah there are limitations to XBAP, such as Reflection etc etc. I think this case is definately one such case. Sad to say. You can try running it in full trust which you can set from inside VisualStudio.

  8. Nilz says:

    I tried your method to add WPF custom controls to WinForms. I have some trouble with it not displaying. I was wondering were you able to add a WPF custom control to a WinForm? Can you only add WPF user controls to WinForms? All examples I have found so far only seem to do this… Could you please post a article perhaps on adding WPF custom controls?

    Cheers,

    Nilz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s