Lesson 28 of 30
WPF and XAML Basics
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
What is WPF?
Windows Presentation Foundation (WPF) is a modern desktop UI framework using XAML (Extensible Application Markup Language) for layouts and data binding for connecting UI to data. WPF supports GPU-accelerated rendering, styles, templates, and animations.
Creating a WPF Project
- File → New → Project → WPF Application.
- Select .NET 9 target framework.
XAML Basics
<!-- MainWindow.xaml -->
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Demo" Width="400" Height="250">
<StackPanel Margin="16">
<TextBlock Text="Enter your name:" Margin="0 0 0 8"/>
<TextBox x:Name="txtName" Margin="0 0 0 8"/>
<Button Content="Greet" Click="BtnGreet_Click"/>
<TextBlock x:Name="lblOutput" FontSize="18" Margin="0 12 0 0"/>
</StackPanel>
</Window>
Code-Behind
// MainWindow.xaml.cs
private void BtnGreet_Click(object sender, RoutedEventArgs e)
=> lblOutput.Text = $"Hello, {txtName.Text}!";
Data Binding
// ViewModel
public class MainViewModel : INotifyPropertyChanged
{
private string _name = "";
public string Name
{
get => _name;
set { _name = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? n = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(n));
}