Поделиться через


TextBlock.Text Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the text contents of a TextBlock.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Text As String
public string Text { get; set; }
<TextBlock>string</TextBlock>
<TextBlock Text="string"/>

Property Value

Type: System.String
A string that specifies the text contents of this TextBlock.
The default is an empty string.

Remarks

Dependency property identifier field: TextProperty

The TextBlock object is the primary text element for displaying text in Silverlight-based applications.

A TextBlock can be thought of as having two object models:

  • A representation as a Text property, which represents only string content. (The text has no formatting other than the formatting that is declared at the TextBlock level.)

  • A representation as an InlineCollection. The collection contains primarily Run objects, each of which can declare its own formatting properties such as FontSize. The Text property returns a value (the appended text of all Run elements in the InlineCollection). However, the returned value does not include any formatting that has been applied to the Run elements. If the InlineCollection is created from XAML as inner text of a TextBlock object element, or if it is created by setting the Text property, the InlineCollection contains a single Run that contains that text.

You can work with either object model. However, if the text consists of series of text elements with individual formatting in an InlineCollection, and if you modify the text by appending to the Text value, you will flatten the previous InlineCollection content and replace it with a single unformatted Run with the new text.

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.

Examples

The following XAML example shows how to define a TextBlock element and set its Text property to a character string.

<TextBlock Text="Hello, world!" />

The following illustration displays the result of the previous XAML content example.

Example results

"Hello, world!" textblock.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

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