Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
TextBlock Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
TextBlock Class

Updated: July 2009

Provides a lightweight control for displaying small amounts of flow content.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
<ContentPropertyAttribute("Inlines")> _
<LocalizabilityAttribute(LocalizationCategory.Text)> _
Public Class TextBlock _
    Inherits FrameworkElement _
    Implements IContentHost, IAddChild, IServiceProvider
Visual Basic (Usage)
Dim instance As TextBlock
C#
[ContentPropertyAttribute("Inlines")]
[LocalizabilityAttribute(LocalizationCategory.Text)]
public class TextBlock : FrameworkElement, IContentHost, 
    IAddChild, IServiceProvider
Visual C++
[ContentPropertyAttribute(L"Inlines")]
[LocalizabilityAttribute(LocalizationCategory::Text)]
public ref class TextBlock : public FrameworkElement, 
    IContentHost, IAddChild, IServiceProvider
JScript
public class TextBlock extends FrameworkElement implements IContentHost, IAddChild, IServiceProvider
XAML Object Element Usage
<TextBlock>
  Inlines
</TextBlock>

Content Model: TextBlock supports the hosting and display of Inline flow content elements. Supported elements include AnchoredBlock, Bold, Hyperlink, InlineUIContainer, Italic, LineBreak, Run, Span, and Underline. See TextBlock Content Model Overview for more information.

TextBlock is designed to be lightweight, and is geared specifically at integrating small portions of flow content into a user interface (UI). TextBlock is optimized for single-line display, and provides good performance for displaying up to a few lines of content.

TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.

Horizontally aligning text within a TextBlock is done with the TextAlignment property. Aligning the TextBlock within the layout of the page is done with the HorizontalAlignment and VerticalAlignment properties.

The following example shows how to use the TextBlock element.

XAML
<TextBlock Name="textBlock1" TextWrapping="Wrap">
  <Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>,
  and is geared specifically at integrating <Italic>small</Italic> portions
  of flow content into a UI.
</TextBlock>
<Button Width="100" Margin="10">Click Me</Button>
<TextBlock  Name="textBlock2" 
  TextWrapping="Wrap" Background="AntiqueWhite" TextAlignment="Center"
>
  By default, a TextBlock provides no UI beyond simply displaying its contents.
</TextBlock>
<Button Width="100" Margin="10">Click Me</Button>

The following figure shows how this example renders.

Screenshot: TextBlocks  and buttons

The following example shows how to shows how to achieve similar results programmatically.

C#
TextBlock textBlock1 = new TextBlock();
TextBlock textBlock2 = new TextBlock();

textBlock1.TextWrapping = textBlock2.TextWrapping = TextWrapping.Wrap;
textBlock2.Background = Brushes.AntiqueWhite;
textBlock2.TextAlignment = TextAlignment.Center;

textBlock1.Inlines.Add(new Bold(new Run("TextBlock")));
textBlock1.Inlines.Add(new Run(" is designed to be "));
textBlock1.Inlines.Add(new Italic(new Run("lightweight")));
textBlock1.Inlines.Add(new Run(", and is geared specifically at integrating "));
textBlock1.Inlines.Add(new Italic(new Run("small")));
textBlock1.Inlines.Add(new Run(" portions of flow content into a UI."));

textBlock2.Text =
    "By default, a TextBlock provides no UI beyond simply displaying its contents.";
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

July 2009

Corrected information about aligning the text in a TextBlock.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
TextBlock does not support AnchoredBlocks      Ifeanyi Echeruo ... Thomas Lee   |   Edit   |   Show History
Adding an anchored block as a direct child of a TextBlock throws an InvalidOperationException
new TextBlock().Inlines.Add(new Figure())

TextBlock treats descendant AnchoredBlocks as simple Runs
new TextBlock().Inlines.Add(new Span(new Figure()))
.      Vladislav Duma ... Thomas Lee   |   Edit   |   Show History
Since TextBlock is derived not from Control, but directly from FrameworkElement, it doesn't support HorizontalContentAlignment and VerticalContentAlignment properties.
TextBlock does not support *ContentAlignment      Vladislav Duma ... DMartin   |   Edit   |   Show History
Since TextBlock is derived not from Control, but directly from FrameworkElement, it doesn't support HorizontalContentAlignment and VerticalContentAlignment properties.

** It does, however, have a TextAlignment property. Since text is its content I'm guessing this is really what you should be looking for.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker