WPF with no XAML

In some sense, computer science is all about layers of abstraction. A language like C# is a layer of abstraction over assembly language, which in turn is a layer of abstraction over machine language.

In some cases the layers of abstraction principle goes too far. I really like WPF style applications, but I am no fan of XAML for creating the UI. While I was eating my lunch today, I decided to see if I could write a simple WPF application that didn’t use XAML.

The result, with just one file of C# code, in my opinion is a much cleaner design than multiple files, including XAML files.


The moral is that some layers of abstraction are necessary in practice. I wouldn’t want to try and write a simple WPF application using assembly language. But layers of abstraction aren’t always a good thing. In addition to XAML, some annoying layers are LINQ, lambda expressions, the entire F# language, the Entity Framework, and AngularJS.

To create my no-XAML WPF demo, I launched Visual Studio and created a new WPF application. Then I deleted files App.xaml and MainWindow.xaml (which also deleted the associated .cs files). Then I added a new C# class named TheWindow. Here’s the code:

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfNoXaml
  public class TheWindow : Window
    private Button button1 = null;
    private TextBox textBox1 = null;

    public static void Main()
      Application app = new Application();
      Window theWindow = new TheWindow();

    public TheWindow()
      this.Width = 500;
      this.Height = 300;

      Grid grid = new Grid();

      button1 = new Button();
      button1.Width = 90;
      button1.Height = 30;
      button1.Content = "Click Me";
      button1.Click +=
        new RoutedEventHandler(button1_Click);
      button1.VerticalAlignment =

      textBox1 = new TextBox();
      textBox1.Width = 200;
      textBox1.Height = 30;
      textBox1.VerticalAlignment =


    void button1_Click(object sender,
      RoutedEventArgs e)
      textBox1.Text = "Thanks!";
This entry was posted in Miscellaneous. Bookmark the permalink.

2 Responses to WPF with no XAML

  1. Hi James,
    Interesting article, what are your concerns with putting UI instructions into an XAML file? I’m completely on board with F#, Entity Framework and Angular JS. I’m a bit fond of linq and lambda when they prohibit realtime modification of code during debug. But when it comes to UI in XAML I disagree. I’ve done a lot of work using the DevExpress XAF framework in the last year or two and I’ve found it a huge timesaver in UI design as well as incredibly powerful (because each user can customize the UI to their needs while the app is running through their override of the default model.diff file). I’d love to understand your concerns with this better.
    Jonathan Humphries

  2. Matt Fenner says:

    IMO the power of XAML is that it makes it much easier to understand the layout of the elements in the UI for someone coming along trying to understand the app. i.e. it is more important for maintainability later than for the first time you create something

Comments are closed.