Text​Block Class

Definition

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

[System.Windows.Localizability(System.Windows.LocalizationCategory.Text)]
[System.Windows.Markup.ContentProperty("Inlines")]
public class TextBlock : System.Windows.FrameworkElement, IServiceProvider, System.Windows.IContentHost
Inheritance
Attributes
Implements

Inherited Members

System.Object

System.Windows.DependencyObject

System.Windows.FrameworkElement

System.Windows.Media.Visual

System.Windows.Threading.DispatcherObject

System.Windows.UIElement

Examples

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.

Screenshot: TextBlocks  and buttons

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.";
Dim textBlock1 As New TextBlock()
Dim textBlock2 As New TextBlock()

textBlock2.TextWrapping = TextWrapping.Wrap
textBlock1.TextWrapping = textBlock2.TextWrapping
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."

Remarks

A TextBlock can contain a string in its Text property or Inline flow content elements, such as Bold, Hyperlink, and InlineUIContainer, in its Inlines property.

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.

Constructors

Text​Block()

Initializes a new instance of the TextBlock class.

Text​Block(​Inline)

Initializes a new instance of the TextBlock class, adding a specified Inline element as the initial display content.

Fields

Background​Property

Identifies the Background dependency property.

Baseline​Offset​Property

Identifies the BaselineOffset dependency property.

Font​Family​Property

Identifies the FontFamily dependency property.

Font​Size​Property

Identifies the FontSize dependency property.

Font​Stretch​Property

Identifies the FontStretch dependency property.

Font​Style​Property

Identifies the FontStyle dependency property.

Font​Weight​Property

Identifies the FontWeight dependency property.

Foreground​Property

Identifies the Foreground dependency property.

Is​Hyphenation​Enabled​Property

Identifies the IsHyphenationEnabled dependency property.

Line​Height​Property

Identifies the LineHeight dependency property.

Line​Stacking​Strategy​Property

Identifies the LineStackingStrategy dependency property.

Padding​Property

Identifies the Padding dependency property.

Text​Alignment​Property

Identifies the TextAlignment dependency property.

Text​Decorations​Property

Identifies the TextDecorations dependency property.

Text​Effects​Property

Identifies the TextEffects dependency property.

Text​Property

Identifies the Text dependency property.

Text​Trimming​Property

Identifies the TextTrimming dependency property.

Text​Wrapping​Property

Identifies the TextWrapping dependency property.

Properties

Background

Gets or sets the Brush used to fill the background of content area.

Baseline​Offset

Gets or sets the amount by which each line of text is offset from the baseline.

Break​After

Gets a LineBreakCondition that indicates how content should break after the current element.

Break​Before

Gets a LineBreakCondition that indicates how content should break before the current element.

Content​End

Gets a TextPointer to the end of content in the TextBlock.

Content​Start

Gets a TextPointer to the beginning of content in the TextBlock.

Font​Family

Gets or sets the preferred top-level font family for the TextBlock.

Font​Size

Gets or sets the top-level font size for the TextBlock.

Font​Stretch

Gets or sets the top-level font-stretching characteristics for the TextBlock.

Font​Style

Gets or sets the top-level font style for the TextBlock.

Font​Weight

Gets or sets the top-level font weight for the TextBlock.

Foreground

Gets or sets the Brush to apply to the text contents of the TextBlock.

Hosted​Elements​Core

Gets an enumerator that can be used iterate the elements hosted by this TextBlock.

Inlines

Gets an InlineCollection containing the top-level Inline elements that comprise the contents of the TextBlock.

Is​Hyphenation​Enabled

Gets or sets a value that indicates whether automatic hyphenation of words is enabled or disabled.

Line​Height

Gets or sets the height of each line of content.

Line​Stacking​Strategy

Gets or sets the mechanism by which a line box is determined for each line of text within the TextBlock.

Logical​Children

Gets an enumerator that can iterate the logical children of the TextBlock.

Padding

Gets or sets a value that indicates the thickness of padding space between the boundaries of the content area, and the content displayed by a TextBlock.

Text

Gets or sets the text contents of a TextBlock.

Text​Alignment

Gets or sets a value that indicates the horizontal alignment of text content.

Text​Decorations

Gets or sets a TextDecorationCollection that contains the effects to apply to the text of a TextBlock.

Text​Effects

Gets or sets the effects to apply to the text content in this element.

Text​Trimming

Gets or sets the text trimming behavior to employ when content overflows the content area.

Text​Wrapping

Gets or sets how the TextBlock should wrap text.

Typography

Gets the currently effective typography variations for the contents of this element.

Visual​Children​Count

Gets the number of Visual children hosted by the TextBlock.

Methods

Arrange​Override(​Size)

Positions child elements and determines a size for the TextBlock.

Get​Baseline​Offset(​Dependency​Object)

Returns the value of the BaselineOffset attached property for a specified dependency object.

Get​Font​Family(​Dependency​Object)

Returns the value of the FontFamilyProperty attached property for a specified dependency object.

Get​Font​Size(​Dependency​Object)

Returns the value of the FontSize attached property for a specified dependency object.

Get​Font​Stretch(​Dependency​Object)

Returns the value of the FontStretch attached property for a specified dependency object.

Get​Font​Style(​Dependency​Object)

Returns the value of the FontStyle attached property for a specified dependency object.

Get​Font​Weight(​Dependency​Object)

Returns the value of the FontWeight attached property for a specified dependency object.

Get​Foreground(​Dependency​Object)

Returns the value of the Foreground attached property for a specified dependency object.

Get​Line​Height(​Dependency​Object)

Returns the value of the LineHeight attached property for a specified dependency object.

Get​Line​Stacking​Strategy(​Dependency​Object)

Returns the value of the LineStackingStrategy attached property for a specified dependency object.

Get​Position​From​Point(​Point, ​Boolean)

Returns a TextPointer to the position closest to a specified Point.

Get​Rectangles​Core(​Content​Element)

Returns a read-only collection of bounding rectangles for a specified ContentElement.

Get​Text​Alignment(​Dependency​Object)

Returns the value of the TextAlignment attached property for a specified dependency object.

Get​Visual​Child(​Int32)

Returns the Visual child at a specified index.

Hit​Test​Core(​Point​Hit​Test​Parameters)

Returns a PointHitTestResult for specified PointHitTestParameters.

Input​Hit​Test​Core(​Point)

Returns the IInputElement at a specified Point within the TextBlock.

Measure​Override(​Size)

Called to re-measure the TextBlock.

On​Child​Desired​Size​Changed​Core(​UI​Element)

Called when a child element deriving from UIElement changes its DesiredSize.

On​Create​Automation​Peer()

Creates and returns an AutomationPeer object for this TextBlock.

On​Property​Changed(​Dependency​Property​Changed​Event​Args)

Called when the value one or more hosted dependency properties changes.

On​Render(​Drawing​Context)

Renders the contents of a TextBlock.

Set​Baseline​Offset(​Dependency​Object, ​Double)

Sets the value of the BaselineOffset attached property on a specified dependency object.

Set​Font​Family(​Dependency​Object, ​Font​Family)

Sets the value of the FontFamily attached property on a specified dependency object.

Set​Font​Size(​Dependency​Object, ​Double)

Sets the value of the FontSize attached property on a specified dependency object.

Set​Font​Stretch(​Dependency​Object, ​Font​Stretch)

Sets the value of the FontStretch attached property on a specified dependency object.

Set​Font​Style(​Dependency​Object, ​Font​Style)

Sets the value of the FontStyle attached property on a specified dependency object.

Set​Font​Weight(​Dependency​Object, ​Font​Weight)

Sets the value of the FontWeight attached property on a specified dependency object.

Set​Foreground(​Dependency​Object, ​Brush)

Sets the value of the Foreground attached property on a specified dependency object.

Set​Line​Height(​Dependency​Object, ​Double)

Sets the value of the LineHeight attached property on a specified dependency object.

Set​Line​Stacking​Strategy(​Dependency​Object, ​Line​Stacking​Strategy)

Sets the value of the LineStackingStrategy attached property on a specified dependency object.

Set​Text​Alignment(​Dependency​Object, ​Text​Alignment)

Sets the value of the TextAlignment attached property on a specified dependency object.

Should​Serialize​Baseline​Offset()

Returns a value that indicates whether the effective value of the BaselineOffset property should be serialized during serialization of a TextBlock object.

Should​Serialize​Inlines(​Xaml​Designer​Serialization​Manager)

Returns a value that indicates whether the effective value of the Inlines property should be serialized during serialization of a TextBlock object.

Should​Serialize​Text()

Returns a value that indicates whether the effective value of the Text property should be serialized during serialization of a TextBlock object.

Explicit Interface Implementations

I​Service​Provider.​Get​Service(​Type)

This method supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

I​Content​Host.​Get​Rectangles(​Content​Element)

This method supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. Use the type-safe GetRectanglesCore(ContentElement) method instead.

I​Content​Host.​Hosted​Elements

This type or member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. Use the type-safe HostedElementsCore property instead.

I​Content​Host.​Input​Hit​Test(​Point)

This method supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. Use the type-safe InputHitTestCore(Point) method instead.

I​Content​Host.​On​Child​Desired​Size​Changed(​UI​Element)

This method supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. Use the type-safe OnChildDesiredSizeChangedCore(UIElement) method instead.

I​Add​Child.​Add​Child(​Object)

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

I​Add​Child.​Add​Text(​String)

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.