i-think Twenty-Two

Now with more coherency.

Adding WPF Windows to an existing Windows Form Project

| Comments

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:

  • Consistent appearance is the first reason that comes to mind. WPF windows look just a little different to Windows Forms (with default settings) and even more different if you apply all the fancy styling options. Of course, if you are looking to create a window with a distinct look, this doesn’t apply
  • Maintainable code is another reason. Supporting multiple types of windowing increases the complexity of your project

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"
&nbsp_place_holder;&nbsp_place_holder;&nbsp_place_holder;xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
&nbsp_place_holder;&nbsp_place_holder;&nbsp_place_holder;xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
&nbsp_place_holder;&nbsp_place_holder;&nbsp_place_holder;Height="300" Width="300">
&nbsp_place_holder;&nbsp_place_holder;&nbsp_place_holder;<grid>  
&nbsp_place_holder;&nbsp_place_holder;&nbsp_place_holder;</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.

Comments