Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
TextBlock Class

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 3

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

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

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
<ContentPropertyAttribute("Inlines", True)> _
Public NotInheritable Class TextBlock _
    Inherits FrameworkElement
Visual Basic (Usage)
Dim instance As TextBlock
C#
[ContentPropertyAttribute("Inlines", true)]
public sealed class TextBlock : FrameworkElement
XAML Object Element Usage
<TextBlock ...>text</TextBlock>
-or-
<TextBlock>
  oneOrMoreInlineElements
</TextBlock>
-or-
<TextBlock .../>

XAML Values

text

Inner text that is displayed in the TextBlock. This text becomes the value of the Text property. It also creates a single new Run object in the Inlines collection that holds the same text.

oneOrMoreInlineElements

One or more Run or LineBreak elements.

The TextBlock control is the primary element for displaying text in Silverlight applications. If you need to display caption for another control that includes required field or validation error indicators, use the Label control.

When you set text in a TextBlock, it is not necessary in XAML to explicitly specify the Text property. You can put text in the TextBlock container as its content, or inner text, as in the following example:

<TextBlock>Hello, world!</TextBlock>
NoteNote:

Leading or trailing whitespace is not preserved when you set the Text property. For details on whitespace handling in XAML, see XAML Overview.

You can align the TextBlock within the layout of a parent container with the TextAlignment, HorizontalAlignment, and VerticalAlignment properties.

The Run and LineBreak objects can also be used to render formatted text, as child elements of a TextBlock in XAML (or as items of Inlines in code). The Run object is a text element that represents a discrete section of formatted or unformatted text. The LineBreak object represents an explicit new line in a TextBlock.

The default value of the FontSize property of a rendered TextBlock is 11 (measured in pixels).

The default value of the FontFamily property of a rendered TextBlock is "Portable User Interface". For more information on the "Portable User Interface" concept, see Text and Fonts.

TextBlock Text Model

Rather than presenting a single string, a TextBlock can also present a series of strings contained in different Run elements, where each element can have different character-level font attributes. The LineBreak object represents an explicit new line in a TextBlock, and is generally used between surrounding Run elements. LineBreak and Run share a base class (Inline), which makes it possible for a TextBlock to hold a strongly typed InlineCollection of its text model content. That InlineCollection is also the TextBlock XAML content property, and the end result is that to specify items in the TextBlock model you simply specify various Run and LineBreak elements as child elements of the TextBlock.

The following XAML example shows how to define several differently formatted text strings in a TextBlock by using Run objects, separated with LineBreak.

<!-- Display formatted text as Run objects within a TextBlock. -->
<Canvas>
<TextBlock
  FontFamily="Arial" Width="400" Text="Sample text formatting runs">
  <LineBreak/>
  <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
  <LineBreak/>
  <Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18" FontStyle="Italic">Times New Roman Italic 18</Run>
  <LineBreak/>
  <Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14" FontWeight="Bold">Verdana Bold 14</Run>
</TextBlock>
</Canvas>

The following illustration shows the rendered formatted text from the previous XAML content example.

TextBlock rendering multiple Run objects

ms617591.JOLT_run_in_xaml_01(en-us,VS.95).png

LineBreak forces the text in each Run to display on a separate line. Without the LineBreak, the text in each Run would flow together as one line and would often be clipped due to exceeding the TextBlock object width or the width of the Silverlight content area. The following illustration shows how the formatted text would render without using LineBreak objects.

TextBlock rendering multiple Run objects without LineBreak objects

ms617591.JOLT_run_in_xaml_02(en-us,VS.95).png

Accessing the TextBlock Text Model in Code

There are really two properties involved in the TextBlock text model: Text, and Inlines. Changing the value of Text is not always advisable, because it can flatten any existing font element formatting on the individual Run items as well as strip the LineBreak elements for a TextBlock that was originally constructed with inlines and not just Text. For details, see Inlines.

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.TextBlock
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker