XAML Controls

Building rich and responsive user interfaces with the Windows App SDK.

Introduction to Windows App SDK XAML Controls

The Windows App SDK provides a comprehensive set of XAML controls designed to enable developers to build modern, beautiful, and performant Windows applications. These controls integrate seamlessly with the WinUI 3 library, offering a consistent look and feel across different Windows versions and devices.

This documentation explores the various categories of XAML controls available, their usage, and common patterns.

Layout Controls

Effective layout is crucial for creating user-friendly applications. The Windows App SDK offers several layout controls to arrange UI elements.

Grid

The Grid control allows you to arrange content in rows and columns, similar to a spreadsheet.

XAML Example:

<Grid RowDefinitions="Auto,*,Auto" ColumnDefinitions="Auto,*">
    <TextBlock Grid.Row="0" Grid.Column="0" Text="Header"/>
    <Border Grid.Row="1" Grid.Column="0" Background="LightGray"/>
    <TextBlock Grid.Row="1" Grid.Column="1" Text="Main Content"/>
    <Button Grid.Row="2" Grid.Column="1" Content="Submit"/>
</Grid>

Key Properties:

  • RowDefinitions: Defines the rows in the grid.
  • ColumnDefinitions: Defines the columns in the grid.

StackPanel

The StackPanel control arranges child elements in a single line, either horizontally or vertically.

XAML Example:

<StackPanel Orientation="Horizontal" Spacing="10">
    <Button Content="Button 1"/>
    <Button Content="Button 2"/>
</StackPanel>

Key Properties:

  • Orientation: Specifies whether to stack elements horizontally or vertically.
  • Spacing: Adds space between child elements.

Input Controls

These controls allow users to interact with your application by providing input.

TextBox

The TextBox control allows users to enter and edit text.

XAML Example:

<TextBox x:Name="NameTextBox" PlaceholderText="Enter your name"/>

Key Properties:

  • Text: The current text content of the TextBox.
  • PlaceholderText: Text displayed when the TextBox is empty.
  • IsReadOnly: Determines if the text can be edited.

Button

The Button control triggers an action when clicked.

XAML Example:

<Button Content="Click Me" Click="OnButtonClick"/>

Key Properties:

  • Content: The content displayed on the button.
  • Click: The event handler for click events.

CheckBox

The CheckBox control represents a Boolean state (checked or unchecked).

XAML Example:

<CheckBox Content="Accept terms and conditions"/>

Key Properties:

  • IsChecked: The current state of the CheckBox.
  • Content: Text displayed next to the CheckBox.

Content Dialogs

Present important information or gather user input in a modal dialog.

ContentDialog

A modal dialog that overlays application content to prompt the user for a decision or input.

XAML Example:

<ContentDialog Title="Confirm Action" PrimaryButtonText="OK" SecondaryButtonText="Cancel">
    <TextBlock Text="Are you sure you want to proceed?"/>
</ContentDialog>

Key Properties:

  • Title: The title of the dialog.
  • PrimaryButtonText: Text for the primary action button.
  • SecondaryButtonText: Text for the secondary action button.
  • IsPrimaryButtonEnabled: Whether the primary button is enabled.

Data Visualization Controls

Visualize data to provide users with insights.

DataGrid

A powerful control for displaying and editing tabular data.

XAML Example:

<DataGrid ItemsSource="{x:Bind MyDataCollection}" AutoGenerateColumns="True"/>

Key Properties:

  • ItemsSource: The collection of data to display.
  • Columns: Defines the columns of the DataGrid.
  • AutoGenerateColumns: Automatically creates columns based on data source properties.

Further Reading

Explore the official documentation for detailed API references, tutorials, and best practices: