Open WPF Windows from Winforms application

Veröffentlicht von

WPF and Winforms can be mixed. In our applications some new dialogs and Windows are made with WPF, the overall application still uses Winforms.

Here is an example how to open WPF windows and dialogs from a Winforms application. Our demo project consists of two projects:

We want to open MyWindow from Form1. For that the WPF project must be referenced in the Winforms application. To open the WPF window we can use the following code:

WPFComponents.MyWindow dlg = new WPFComponents.MyWindow();           
WPFHelper.SetOwner(this, dlg);

This code does not work out of the box:

The first thing we do is to add some references concerning WPF to our Winforms project.

This should fix everything except the WPFHelper. This is a small helper class we create:

public class WPFHelper
    /// <summary>
    /// Sets the owner of a wpf window to a winforms
    /// </summary>
    /// <param name="ownerForm"></param>
    /// <param name="window"></param>
    public static void SetOwner(System.Windows.Forms.Form ownerForm, System.Windows.Window window)
        WindowInteropHelper helper = new WindowInteropHelper(window);
        helper.Owner = ownerForm.Handle;

And that’s it! This allows opening WPF windows from the Winforms application.

Download of the example project

Ein Kommentar

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.