Page 3 of 6
WPF without XAML
It is a much over looked fact that if you don’t want to use XAML then you don’t have to.
There are still .NET classes for each and every WPF component that you care to use.
For example as well as there being a <Button> tag there is a Button class within the framework (not to be confused with the Windows Forms Button class). In fact there has to be a framework class for everything in XAML because XAML is just an object creator and initialisation language. XAML is always converted to framework objects, properties and methods. What this means is that, if we want to, we can create the same objects and work with their properties and methods without any XAML.
If you want to code our first example using minimal XAML you can first delete the grid tags and everything between them and then enter the following code in the code page within the Window1 class definition:
public Button Button1;
Grid g = new Grid();
Button1 = new Button();
Button1.Height = 50;
Button1.Width = 200;
Button1.Content = "Click Me";
Button1.Click += OnClick1;
this.Content = g;
void OnClick1(object sender,
Button1.Content = "Hello World";
It looks more complicated and it’s longer but you should be able to see that it does the same job as the XAML by creating objects and setting their properties.
So use XAML or don’t use XAML, the end result is the same. In the rest of this article the focus will be on using XAML but converting any of the examples into pure code is relatively easy.