Adding WPF Windows to an existing Windows Form Project

I’ve been recently reading Essential Windows Presentation Foundation (Chris Anderson) and I have been really looking forward to being able to create some funky WPF apps. I’ve almost finished (except for the sudden scope creep) a utility that I’ve been writing using Windows Forms. Like most good utilities it sits in the system tray just waiting to be used.

While using this tool yesterday I realised that I was being driven crazy by its need for interaction by the mouse. So I decided to create a global shortcut key and that would open a little navigation window to help you select the item you wanted (this is just some of the aforementioned scope creep).

I knew straight away that this was a job perfectly suited for WPF. So, I opened up the Add New Item dialog in Visual Studio 2008, selected WPF and was greeted with one solitary option:
Add New Item (WPF)

Unfortunately when you have a Windows Form project, User Control (WPF) is the only choice you are given. Fortunately, there is a workaround, but let’s first look at some reasons why mixing WPF into a windows form application might not be the best idea:

There may be others and hopefully someone will point them out in the comments.

But now back to adding a WPF window into your Windows Forms project. Without the Window option, begrudgingly select User Control (WPF). Visual Studio then adds all the necessary references to your project and create a new XAML file and a corresponding CS file (or VB if you are that way inclined).

It turns out that User controls and Windows are very similar, so in your XAML file, change the “UserControl” tag to “Window” (remember to also update the closing tag). Your XAML should now look something like this:


<window x:Class="MyProject.UserControl1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Height="300" Width="300">
   <grid>

   </grid>
</window>

Finally update your code file to remove the UserControl inheritance. (It’s already defined in your XAML anyway).

///

/// Interaction logic for UserControl1.xaml
/// 

public partial class UserControl1
{
    public UserControl1()
    {
        InitializeComponent();
    }
}

Then you can play with the WPF window as you would expect.

  • Andre Vileski

    Hi,

    Thanks for this tip very useful, now if the winform that you wish to replace with a WPFForm is your first form to be shown how do you modify the main function :

    static void Main()
    {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
    
       Application.Run(new FrmLogon()); // This won't work with a WPF form
    }
    
  • charan

    How can i set MdiParent to this WPF window?

  • http://pulse.yahoo.com/_LO3EKC2HQABPX3MXAVF53WNXPY midspace

    There is a small issue when you open a WPF window from within your WinForm application.n”window.ShowDialog();” will work without any issues.n”window.Show();” will cause problems however.nnYou must call :n”System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(window);”"window.Show();”nnOtherwise the Windows Form applicaiton will catch all the keyboard input.