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.

Text Wrapping

The normal configuration for a TextBlock causes all of the text to appear in a single line. Often you will want the text to wrap at the edge of a TextBlock to create multiple lines. You can control this behaviour with the TextWrapping property. This accepts a value from the TextWrapping enumeration, though when using XAML you provide a string that matches one of the named constants. Three options are available:

  • NoWrap. This is the default setting, causing all of the text to appear on one line.
  • Wrap. When you use the Wrap option, a word wrapping algorithm is applied. When a line of text is too long to fit within the available horizontal area, the algorithm looks for a suitable place to split the line. This is usually at a whitespace or punctuation character. If no suitable position exists on the current line, the line is wrapped in such a way as to split a word. This ensures that long words will not be clipped at the edge of the TextBlock.
  • WrapWithOverflow. If you select the WrapWithOverflow option, the lines of text will be wrapped at the position of whitespace or punctuation characters, as with the Wrap setting. The key difference is how the wrapping algorithm deals with lines with no suitable wrapping point. Long words will never be split over two lines. Instead, they will extend beyond the edge of the TextBlock and be clipped.

Try modifying the opening tag of the TextBlock to add wrapping, as follows:

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

Run the program to see the results. You can see that the wrapping algorithm chooses appropriate positions to split lines. As you resize the window, the wrapping algorithm is executed repeatedly to reposition the words according to the available width.

WPF TextBlock control with word wrapping

Text Alignment

TextBlocks support four options for the horizontal alignment of their content. By default, all text is aligned to the left of the TextBlock. You can instead align the text to the right or centrally. You can also elect to justify the text. This means that additional space is inserted between words so that both the left and right edges of each line are aligned, except for the last line of a paragraph.

To set the alignment, use the TextAlignment property. This accepts a value from the TextAlignment enumeration when set from code. In XAML, set the value to "Left", "Right", "Center" or "Justify".

Modify the TextBlock as follows to justify the text:

<TextBlock Foreground="SeaGreen"

If you run the program you will see that the text is spread out to fill the width of the TextBlock.

WPF TextBlock control with justified text

Font Formatting

There are a number of properties defined within TextBlock that allow you to change the appearance of the rendered characters. These are normally provided by the font properties of the Control class but are recreated in TextBlock and behave in a similar manner. They are:

  • FontFamily. Allows you to choose the typeface for the text.
  • FontSize. Lets you set the height of the text in pixels, inches, centimetres or points.
  • FontStretch. Expands or contracts the typeface horizontally, where the selected font permits.
  • FontStyle. Lets you apply italics to the text or use an oblique font.
  • FontWeight. Determines how heavy or light the strokes of the characters appear.

To apply some of these properties, adjust the opening tag of the TextBlock as shown below:

<TextBlock Foreground="SeaGreen"
           FontFamily="Times New Roman"

Running the program shows the updated font styles.

WPF TextBlock control with formatted text

26 December 2013