C# > UI Programming > WPF > XAML Basics
Simple WPF Window with a Button
This snippet demonstrates the basic structure of a WPF application using XAML to define the user interface. It creates a simple window containing a button.
XAML Structure
This XAML code defines a `Window` as the root element. The `xmlns` and `xmlns:x` attributes declare the XML namespaces required for WPF. The `Title` property sets the window's title bar text. `Height` and `Width` define the initial window size. Inside the `Window`, a `Grid` panel is used for layout. A `Button` is placed within the `Grid`, centered both horizontally and vertically, and its content is set to "Click Me!".
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My First WPF Window" Height="350" Width="600">
<Grid>
<Button Content="Click Me!" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
C# Code-Behind
This C# code represents the code-behind for the XAML. `MainWindow` class inherits from `Window`. The `InitializeComponent()` method, called in the constructor, links the XAML and the C# code, and initializes the UI elements defined in XAML. This ensures that the visual elements declared in XAML are instantiated and associated with their corresponding C# objects.
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Explanation of Key XAML Elements
Concepts Behind the Snippet
Real-Life Use Case
This basic structure forms the foundation for any WPF application. You can expand upon it to create more complex UIs by adding more controls, layout panels, and data binding.
Best Practices
Interview Tip
Be prepared to explain the difference between XAML and code-behind, and the role of layout panels in WPF. Understanding the MVVM pattern is also crucial for demonstrating your WPF knowledge.
When to use them
XAML is a better option if you want to make your application more maintainable, testable, and extensible. It is also a good choice if you want to create a more complex UI.
Alternatives
WinForms: A Windows Forms Application is a desktop app platform designed for building Windows apps. This is the previous UI framework and is not recomended. AvaloniaUI: Avalonia is a cross-platform UI framework for .NET. It allows to write UI once and have it run on Windows, macOS, Linux, iOS, Android and WebAssembly.
Pros
Cons
FAQ
-
What is XAML?
XAML (Extensible Application Markup Language) is an XML-based markup language used to define the user interface in WPF applications. It separates the UI design from the application's logic. -
What is the purpose of the code-behind file?
The code-behind file (e.g., `MainWindow.xaml.cs`) contains the C# code that handles the application's logic, such as event handlers, data access, and business rules. It interacts with the UI elements defined in XAML. -
How do I handle a button click event?
You can handle a button click event by adding a `Click` event handler to the `Button` element in XAML and defining the corresponding method in the code-behind file. For example: XAML: `` C#: `private void Button_Click(object sender, RoutedEventArgs e) { // Your code here }`