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>
Note: |
|---|
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
.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
.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.