Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The TextBlock control provides flexible text support for WPF applications. Target this element primarily for basic UI scenarios that don't require more than one paragraph of text. It supports properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping. Add text content using the Text property. When you use it in XAML, content between the open and closing tag becomes the text of the element.
Set text content
Set text content in a TextBlock using two approaches:
Text property: For simple, uniformly formatted text, use the Text property. This approach is straightforward and applies the same formatting to all text.
<TextBlock Text="This is simple text." Margin="0,0,0,20"/>Inlines property: For text with mixed formatting, use the Inlines property. This collection holds inline elements that let you apply different formatting to different parts of the text.
<TextBlock Margin="0,0,0,20"> This text contains <Bold>bold</Bold> and <Italic>italic</Italic> formatting. </TextBlock>
Inline elements and formatting
The Inlines property accepts a collection of Inline elements that enable rich text formatting within a single TextBlock. Common inline elements include:
- Run: Represents a run of unformatted or uniformly formatted text.
- Span: Groups other inline elements and applies formatting to all contained elements.
- Bold: Applies bold formatting to contained text.
- Italic: Applies italic formatting to contained text.
- Underline: Applies underline formatting to contained text.
- Hyperlink: Creates clickable hyperlinks within the text.
- LineBreak: Inserts a line break.
This example demonstrates using inline elements:
<TextBlock Margin="0,0,0,20">
<Run FontWeight="Bold" Foreground="Blue">Welcome</Run>
<Run> to </Run>
<Span FontFamily="Courier New">
<Run>formatted</Run>
<Run Foreground="Red"> text</Run>
</Span>
<Run>!</Run>
</TextBlock>
You can also manipulate the Inlines collection programmatically to build formatted text dynamically:
TextBlock textBlock = new TextBlock();
textBlock.Inlines.Add(new Bold(new Run("Important:")));
textBlock.Inlines.Add(new Run(" This is a message."));
Dim textBlock As New TextBlock()
textBlock.Inlines.Add(New Bold(New Run("Important:")))
textBlock.Inlines.Add(New Run(" This is a message."))
Text wrapping
The TextWrapping property controls how text behaves when it reaches the edge of the TextBlock. Set it to one of the TextWrapping enumeration values:
NoWrap: Text doesn't wrap and extends beyond the TextBlock if necessary.Wrap: Text wraps to the next line when it reaches the edge.WrapWithOverflow: Text wraps if possible, but if a single word is too long, it extends beyond the boundary.
<TextBlock TextWrapping="Wrap" Width="200" Margin="0,0,0,20">
This text will wrap to multiple lines when it reaches the width of 200 pixels.
</TextBlock>
Text wrapping is particularly useful for responsive layouts and when displaying dynamic content where the text length is unknown.
Typography and OpenType features
WPF TextBlock supports advanced OpenType typography features through the Typography class. These features enable fine-grained control over text rendering, including:
- Ligatures (combining characters like "fi" into a single glyph)
- Kerning (adjusting spacing between specific character pairs)
- Stylistic alternates
- Small caps
- Swashes and ornamental characters
Access typography features through attached properties:
<TextBlock Typography.Kerning="True"
Typography.Capitals="SmallCaps"
Margin="0,0,0,20">
Typography Features
</TextBlock>
To set typography features in code, use the static methods on the Typography class:
TextBlock textBlock = new TextBlock();
textBlock.Text = "Typography Features";
Typography.SetKerning(textBlock, true);
Typography.SetCapitals(textBlock, FontCapitals.SmallCaps);
Dim textBlock As New TextBlock()
textBlock.Text = "Typography Features"
Typography.SetKerning(textBlock, True)
Typography.SetCapitals(textBlock, FontCapitals.SmallCaps)
For more information about typography features, see Typography in WPF.
Hyperlinks
Add clickable hyperlinks within a TextBlock using the Hyperlink element. Hyperlinks support navigation to URIs and can respond to click events.
<TextBlock Margin="0,0,0,20">
For more information, visit
<Hyperlink NavigateUri="https://learn.microsoft.com">
Microsoft Learn
</Hyperlink>.
</TextBlock>
Handle hyperlink navigation events to control behavior:
<TextBlock>
<Hyperlink NavigateUri="https://example.com"
RequestNavigate="Hyperlink_RequestNavigate">
Click here
</Hyperlink>
</TextBlock>
In the code-behind, handle the RequestNavigate event to open the link in the default browser:
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo
{
FileName = e.Uri.AbsoluteUri,
UseShellExecute = true
});
e.Handled = true;
}
Private Sub Hyperlink_RequestNavigate(sender As Object, e As RequestNavigateEventArgs)
Process.Start(New ProcessStartInfo With {
.FileName = e.Uri.AbsoluteUri,
.UseShellExecute = True
})
e.Handled = True
End Sub
TextBlock vs RichTextBox
While TextBlock is ideal for displaying read-only text with formatting, consider using RichTextBox when you need:
- Multiple paragraphs with different formatting
- User editing capabilities
- Text indentation
- More complex document structures
TextBlock provides better performance and a simpler programming model, making it the preferred choice for most UI text display scenarios.
Reference
Related Sections
.NET Desktop feedback