BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Information Controls - TextBlock Basics

The forty-first part of the Windows Presentation Foundation Fundamentals tutorial begins to look at another of the WPF user information controls. This time it's the TextBlock control, which allows the display of short text items with formatting.

TextBlock

The TextBlock class provides a means for displaying small amounts of text within Windows Presentation Foundation (WPF) applications. In many cases the text will be a single line or a few short lines; TextBlocks are not suitable for large documents.

You might think that TextBlocks and Labels provide very similar functionality. Indeed, if you examine the visual tree of a simple Label, you will find that it uses a TextBlock for display purposes. However, there are some key differences between the two controls.

A TextBlock is a lightweight control that uses fewer resources than a Label but offers less functionality. Importantly, TextBlock is a subclass of FrameworkElement. This is quite high in the inheritance hierarchy of WPF controls. It means that TextBlocks do not have a replaceable template and can only be bound to text, unlike a Label. TextBlocks do not support shortcut keys, or mnemonics, and do not "grey out" automatically when disabled.

To offset these limitations, TextBlocks do include flexible formatting options. For example, you can set parts of the text in a single TextBlock to be bold or italicised. You can also include line breaks and use automatic word-wrapping algorithms for longer sentences or paragraphs.

In this article we will look at the basics of TextBlocks, seeing how to add and format text. In the next instalment of the tutorial we will examine the ability to add manual line breaks and include multiple sections of text, each with different formatting options.

Example TextBlock

To begin, we need a sample project. Create a new WPF application in Visual Studio naming the solution, "TextBlockDemo". Once created, replace the XAML of the main window with the code below. This window contains a StackPanel, which holds a single TextBlock.

<Window x:Class="TextBlockDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlock Demo"
        Width="250"
        Height="200">
    <StackPanel>
        <TextBlock>
            The Mermaid
            Far out at sea the water is as blue as the bluest cornflower,
            and as clear as the clearest crystal;
            but it is very deep, too deep for any cable to fathom,
            and if many steeples were piled on the top of one another
            they would not reach from the bed of the sea to the surface
            of the water. It is down there that the Mermen live.
        </TextBlock>
    </StackPanel>
</Window>

When you run the program you should see the results below. Even though the text in the XAML included line breaks, the results do not. This is because XAML is parsed using normal XML rules.

WPF TextBlock control

Foreground and Background

Many WPF controls include Foreground and Background properties, which allow you to apply a Brush to these elements. Brushes are often solid colours but can include gradient fills, patterns and other effects. As we've seen in the article, "WPF Base Classes - Control - Visual Properties", these properties are defined in the Control class. TextBlock is not a subclass of Control, so does not inherit these properties. However, it does implement them independently.

NB: The fact that TextBlock does not inherit from Control leads some people to say that TextBlock is not a WPF control. However, Microsoft describes it as a lightweight control in the MSDN documentation, so I will use this terminology.

To change the two colours, modify the opening XAML tag for the TextBlock as shown below. This changes the text colour to SeaGreen and the background of the control to SeaShell, which seem appropriate for the sample text.

<TextBlock Foreground="SeaGreen" Background="SeaShell">

The resultant window appears as follows. Note that the SeaShell colour fills the rectangular TextBlock area only. The remainder of the containing StackPanel is rendered using the default colour.

WPF TextBlock control with foreground and background colours

26 December 2013