C# > UI Programming > WPF > XAML Basics

Adding a TextBlock and Styling in XAML

This snippet demonstrates how to add a TextBlock control to display text in a WPF window and how to style it using XAML attributes.

XAML Code for TextBlock and Styling

This XAML code defines a `Window` containing a `Grid` panel and a `TextBlock` control. The `Text` property of the `TextBlock` is set to "Hello, WPF!". The `FontSize`, `FontWeight`, and `Foreground` attributes are used to style the text. The `HorizontalAlignment` and `VerticalAlignment` properties center the text within the `Grid`.

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlock Example" Height="200" Width="300">
    <Grid>
        <TextBlock Text="Hello, WPF!" FontSize="24" FontWeight="Bold" Foreground="DarkBlue" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Window>

Explanation of TextBlock Properties

  • Text: Specifies the text to be displayed in the `TextBlock`.
  • FontSize: Sets the size of the text.
  • FontWeight: Defines the weight (e.g., Bold, Normal) of the text.
  • Foreground: Sets the color of the text.
  • HorizontalAlignment and VerticalAlignment: Control the alignment of the `TextBlock` within its parent container.

Concepts Behind the Snippet

  • Inline Styling: XAML allows you to style UI elements directly within the XAML code using attributes.
  • Text Formatting: WPF provides various properties for formatting text, such as font size, font weight, color, and more.

Real-Life Use Case

TextBlocks are used extensively in WPF applications to display static or dynamic text. They can be used in labels, descriptions, headings, and other UI elements.

Best Practices

  • For more complex styling, consider using Styles and Resources to define reusable styles that can be applied to multiple UI elements.
  • Use data binding to dynamically update the text in a `TextBlock` based on changes in your application's data.

When to use them

TextBlock is usefull for displaying static text, labels or informational content. Also to display dynamic values from data-bindings

Alternatives

Label control: Label is designed to display a short, non-editable text. TextBox control: TextBox allows the user to enter and edit text.

Pros

  • Simple to Use: TextBlock is a straightforward control for displaying text.
  • Customizable: Provides various properties for customizing the appearance of the text.

Cons

  • Limited Editing: TextBlock is primarily for displaying text and does not support direct user editing.
  • No Input Validation: Since it's for display, it lacks input validation capabilities.

FAQ

  • How can I change the text of the TextBlock programmatically?

    You can change the text of the `TextBlock` in the code-behind by accessing it by its name (if you assigned one using the `x:Name` attribute) and setting its `Text` property. For example: XAML: `` C#: `myTextBlock.Text = "New Text";`
  • How can I add a border around the TextBlock?

    You can add a border around the `TextBlock` by wrapping it in a `Border` control and setting the `BorderBrush` and `BorderThickness` properties. For example: ``