How to Use WPF: Build Your First Windows App
In this article
Windows Presentation Foundation (WPF) is Microsoft's desktop UI framework for Windows, built on .NET. To use it, install Visual Studio with the .NET desktop development workload, create a new WPF Application project, and edit the generated XAML file and its code-behind to lay out controls and handle events. That is the whole loop: XAML describes the interface, C# (or VB) drives the behaviour, and the build produces a normal Windows .exe. The steps below take you from a blank machine to a running window you understand line by line.
What You'll Need
- A PC running Windows 10 or Windows 11. WPF is Windows-only; it does not run on macOS or Linux.
- The .NET SDK, which ships with Visual Studio. WPF is supported on .NET (the modern cross-version runtime) and on the older .NET Framework; new projects should target .NET.
- Visual Studio, free Community edition is fine, with the .NET desktop development workload ticked during installation. This workload includes the WPF project templates and the XAML designer.
- No prior XAML knowledge required, but basic C# helps: variables, methods, and the idea of a class.
Step-by-Step
- Install the workload. Open the Visual Studio Installer, click Modify on your installation, tick .NET desktop development, and let it finish. If you skip this, the WPF templates will not appear in the New Project dialog. Menu labels can differ slightly between Visual Studio versions.
- Create the project. Launch Visual Studio, choose Create a new project, search for WPF Application, and pick the C# template (not the .NET Framework one unless you specifically need it). Give it a name such as HelloWpf, choose a folder, and click Create.
- Look at the generated files. In Solution Explorer you will see App.xaml (application-level resources and the startup file), MainWindow.xaml (the window's layout), and MainWindow.xaml.cs (the code-behind). Double-click MainWindow.xaml to open it.
- Read the XAML. The file contains a
<Window>root element with a<Grid>inside. XAML is XML: every element is a control, and attributes are properties. Thex:Classattribute links this file to the partial class in the code-behind, which is how the two halves stay connected. - Add controls. Replace the empty Grid contents with a simple layout: a
<StackPanel>holding a<TextBlock>, a<TextBox x:Name="NameBox">, a<Button Content="Say hello" Click="HelloButton_Click"/>, and a second<TextBlock x:Name="OutputText">. Thex:Namegives you a field you can reference from code. - Write the event handler. Open MainWindow.xaml.cs. Inside the window class, add a method named
HelloButton_Clickwith the signatureprivate void HelloButton_Click(object sender, RoutedEventArgs e). Inside it, setOutputText.Text = "Hello, " + NameBox.Text;. The name must match the Click attribute exactly, or the XAML will not compile. - Set window properties. Back in the XAML, adjust
Title,Height,Width, and optionallyWindowStartupLocation="CenterScreen"on the Window element. These are plain properties, so you can also set them in code if you prefer. - Run it. Press F5 to build and debug, or Ctrl+F5 to run without the debugger. A window appears; type a name, click the button, and the second TextBlock updates. If the build fails, the Error List window names the file and line.
- Style it with a resource. Add a
<Window.Resources>block containing a<Style TargetType="Button">with a Setter forPaddingandFontSize. Styles defined here apply to every matching control in the window, which is the core of WPF's separation between look and logic. - Try data binding. Instead of writing to the TextBlock directly, you can bind it:
Text="{Binding ElementName=NameBox, Path=Text}". This makes the output update as the user types, with no event handler at all. Binding is the feature that separates WPF from older WinForms-style coding. - Publish. When the app works, right-click the project and choose Publish. For a simple shareable build, pick Folder and a self-contained or framework-dependent deployment. Self-contained bundles the runtime so the target PC needs nothing installed, at the cost of a larger folder.
Common Mistakes
The most frequent problem is a name mismatch: the Click attribute in XAML must match the handler method name and signature in the code-behind, or the project will not build. Second is forgetting x:Name on a control you want to reach from code, which leaves the compiler unable to find the field. Third is editing the wrong file, changing generated code in the obj folder instead of the source XAML. Fourth is stacking controls without a layout panel: WPF positions children through panels such as Grid, StackPanel and DockPanel, not absolute coordinates, so dropping controls straight into a Window without a panel produces confusing results. Finally, many beginners expect the XAML designer to be pixel-perfect; it is a preview, and the running app is the truth.
Tips
Use a Grid with row and column definitions for anything beyond a simple vertical list; it scales cleanly when the window resizes. Name only the controls you actually reference in code, since every name becomes a field. Keep styling in resources or separate resource dictionaries rather than repeating attributes on each control. Learn the difference between StaticResource and DynamicResource early: static is resolved once and is faster, dynamic updates when the resource changes. Use the Live Visual Tree and Live Property Explorer while debugging to inspect the running UI. And lean on data binding plus the MVVM pattern as your app grows; putting logic in code-behind works for small tools but becomes hard to test at scale.
Troubleshooting
If the WPF template is missing, the .NET desktop development workload is not installed; rerun the Visual Studio Installer and add it. If the build fails with a XAML error, read the first error only, since later ones are often cascades, and check that tag names are closed and attribute quotes balanced. If the app starts and immediately closes, an unhandled exception in the startup path is the usual cause; run with the debugger attached (F5) so it breaks on the exception. If a control does not appear, check whether it is inside a panel that gives it zero size, or whether its Visibility is collapsed. If the designer shows a blank surface but the app runs, the designer failed to load; rebuilding usually clears it. If publishing produces an app that will not start on another PC, confirm whether you chose framework-dependent (target needs the .NET runtime) or self-contained.
Frequently Asked Questions
Is WPF still supported?
Yes. WPF is supported on .NET and receives updates alongside it. It is Windows-only and Microsoft's newer desktop frameworks exist, but WPF remains a fully supported choice for Windows desktop apps.
Do I need Visual Studio, or can I use VS Code?
You can build WPF with the .NET command line and edit XAML in VS Code, but you lose the XAML designer, the visual debugger tools, and the template gallery. Visual Studio is the practical choice for learning.
What is the difference between XAML and C#?
XAML declares the interface: controls, layout, styles and bindings. C# handles behaviour: events, data access and logic. They are two halves of the same partial class.
Can WPF apps run on Mac or Linux?
No. WPF depends on Windows graphics APIs. For cross-platform desktop UI, Microsoft's alternative is .NET MAUI, which is a different framework with a different learning curve.
Should I learn WinForms or WPF first?
If your goal is modern, styled, data-driven desktop apps, learn WPF. WinForms is simpler for quick internal tools but has far less flexibility in layout and styling.
Why does my button click do nothing?
Almost always the handler name in the Click attribute does not match the method in code-behind, or the method has the wrong signature. Check the Error List after building.