Provides a lightweight control for displaying small amounts of flow content.
<ContentPropertyAttribute("Inlines")> _ <LocalizabilityAttribute(LocalizationCategory.Text)> _ Public Class TextBlock _ Inherits FrameworkElement _ Implements IContentHost, IAddChild, IServiceProvider
Dim instance As TextBlock
[ContentPropertyAttribute("Inlines")] [LocalizabilityAttribute(LocalizationCategory.Text)] public class TextBlock : FrameworkElement, IContentHost, IAddChild, IServiceProvider
[ContentPropertyAttribute(L"Inlines")] [LocalizabilityAttribute(LocalizationCategory::Text)] public ref class TextBlock : public FrameworkElement, IContentHost, IAddChild, IServiceProvider
public class TextBlock extends FrameworkElement implements IContentHost, IAddChild, IServiceProvider
<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 and vertically aligning text within a TextBlock is done with the HorizontalContentAlignment and VerticalContentAlignment properties. 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.
<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.
The following example shows how to shows how to achieve similar results programmatically.
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.";
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003