It occurred to me recently that we haven't documented how to create a WPF project without the Fidalgo project templates. Since most of you probably don't have Fidalgo (aka, "Visual Studio 2005 Extensions for WinFX") installed, it might be rather difficult to actually get any WPF code written. Here's a little walkthrough I wrote up this afternoon to help out. I basically reverse-engineered a project that was created with Fidalgo and jacked it into a Windows Forms project. Hope you like.

The first step is to create the application project. You will modify this project to handle WPF content.

1. Create a Windows Application project called "ManuallyCreatingAWpfProject".

2. In Solution Explorer, add references to the following WPF assemblies.

  • PresentationCore
  • PresentationFramework
  • WindowsBase

The default location for these assemblies is C:\Program Files\Reference Assemblies\Microsoft\WinFX\v3.0.

Now use Solution Explorer to create WPF files.

1. In Solution Explorer, right-click on ManuallyCreatingAWpfProject and select Add, then select New Item. In the Add New Item dialog box, select Text File. Name the new file Window1.xaml. This file will hold the code for the WPF user interface. Click the Add button to create the new file.

2. Right-click on the project and select Add, then select New Item. In the Add New Item dialog box, select Class. Name the new file Window1.xaml.cs. Click the Add button to create the new file.

3. Right-click on the project and select Add, then select New Item. In the Add New Item dialog box, select Text File. Name the new file MyApp.xaml. This file will hold the WPF application definition. Click the Add button to create the new file.

4. Right-click on the project and select Add, then select New Item. In the Add New Item dialog box, select Class. Name the new file MyApp.xaml.cs. Click the Add button to create the new file.

5. Delete Program.cs from the project.

Now you have all the files in the project that will hold WPF code. Next you'll define the content for the WPF application by copying the following code into the newly created files.

1. In Solution Explorer, double-click Window1.xaml to open it in the Code Editor.

2. Paste the following code into Window1.xaml.

   <Window x:Class="ManuallyCreatingAWpfProject.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   >
      <Canvas>
         <Button Content="Simple Window"/>
      </Canvas>
   </Window>
 
This code creates a simple window that displays a System.Windows.Controls.Button control.

3. In the Properties Window, change the file's Encoding property to UTF-8.

4. Open Window1.xaml.cs in the Code Editor.

5. Paste the following code into Window1.xaml.cs.

   using System;
   using System.Windows;
   using System.Windows.Controls;
   using System.Windows.Data;
   using System.Windows.Documents;
   using System.Windows.Media;
   using System.Windows.Navigation;
   using System.Windows.Shapes;
 
   namespace ManuallyCreatingAWpfProject
   {
      /// <summary>
      /// Interaction logic for Window1.xaml
      /// </summary>
      public partial class Window1 : Window
      {
         public Window1()
         {
            InitializeComponent();
         }
      }
   }

6. Open MyApp.xaml in the Code Editor.

7. Paste the following code into MyApp.xaml.

   <Application x:Class="ManuallyCreatingAWpfProject.MyApp"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Startup="AppStartup"
   >
      <Application.Resources>
 
      </Application.Resources>
   </Application>

This code defines the WPF System.Windows.Application.

8. In the Properties Window, change the file's Encoding property to UTF-8.

9. Open MyApp.xaml.cs in the Code Editor.

10. Paste the following code into MyApp.xaml.cs.

   using System;
   using System.Windows;
   using System.Data;
   using System.Xml;
   using System.Configuration;
 
   namespace ManuallyCreatingAWpfProject
   {
      /// <summary>
      /// Interaction logic for MyApp.xaml
      /// </summary>
      public partial class MyApp : Application
      {
         void AppStartup(object sender, StartupEventArgs args)
         {
            Window1 mainWindow = new Window1();
            mainWindow.Show();
         }
      }
   }

Now all you need to do is manually edit the project file to make it compatible with the WPF build process.

1. In Solution Explorer, right-click on ManuallyCreatingAWpfProject and select Unload Project.
The project appears in gray and is labeled (unavailable).

2. Right-click on the project and select Edit ManuallyCreatingAWpfProject.csproj.
The project file opens in the Code Editor.

3. Find the following line.

   <None Include="Window1.xaml" />

4. Change this line as follows.

   <Page Include="Window1.xaml" />

5. Find the following line.

   <None Include="MyApp.xaml" />

6. Change this line as follows.

   <ApplicationDefinition Include="MyApp.xaml" />

7. Find the following line.

   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

8. Add the following line after the previous one.

   <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />

9. Save the project file and close it.

10. In Solution Explorer, right-click on ManuallyCreatingAWpfProject and select Reload Project.

11. Press F5 to build and run the application.

That's all there is to it. There's even a form from the Windows Forms world available in case you want to do some interop.

Hope that's helpful.