It can be helpful to start from the beginning when working with new technologies. This post explains how to create a minimal WPF application that produces a single window with a gradient in it, as shown in Figure 1. The point of this exercise is to build the app from scratch, choosing File | New Project | Empty Project rather than File | New Project | WPF Application. The benefit of this exercise is to simply see what ingredients go into the production of a minimal WPF program.



Figure 1: A Simple WPF window with a gradient.

  1. Begin by choosing File | New Project | Empty Project from the Visual Studio 2008 menu.
  2. Choose Project | Add Class from the menu and add a new C# class called Program.
  3. Right click on the References section in the Solution Explorer and remove System.Data and System.Xml, but leave System. Add the following References as shown in Figure 2:
    1. PresentationCore
    2. PresentationFramework
    3. WindowBase
  4. Remove System.Linq and add the following to the program's using statements
    1. System.Windows
    2. System.Windows.Media
  5. Use the svm snippet to add a Main method to your code and use the ctor snippet to add a constructor to your code
    1. Put the [STAThread] attribute above the main method
  6. Fill in the main method and the constructor as shown in Figure 1
  7. Select from the menu Project | Project Properties and set the output type to Windows Application
  8. Have class Program derive from class Window
    1. class Program: Window
  9. Optionally right click on the editor and choose Organize Usings | Remove and Sort from the menu
  10. Press F5 to run the program. You should see the window shown in Figure 1.



Figure 2: The References section from the SimpleWpf project.

The complete source code to this project is shown in Listing 1. You can see that an Application object is created in the Main method, and that a few minimal fields of the window are filled out in the constructor. I also create a WPF LinearGradientBrush and set it as the Background for the window.

using System;
using System.Windows;
using System.Windows.Media;

namespace Project1
    class Program: Window
        static void Main(string[] args)
            Application application = new Application();
            application.Run(new Program());

        public Program()
            Width = 320;
            Height = 260;
            Title = "A Colorful Window";
            Background = new LinearGradientBrush(Colors.AliceBlue, Colors.Aquamarine,
                new Point(0, 0), new Point(1, 1));   

I should perhaps end by reminding you that the simplest way to create a WPF application in Visual Studio is to choose File | New Project | WPF Application. I have shown you this alternative technique simply because I hope you find it interesting or entertaining. It also illustrates that it is possible, though not necessarily recommended, to build WPF applications without using XAML.

The complete Source is on my LINQ Farm. Here is direct link to the download.

kick it on