Udostępnij za pośrednictwem


Text (TextElement)

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

Gets or sets the text content of a TextBlock or Run.

<object Text="string"  .../>
-or-
<object>string<object>
value = object.Text
object.Text = value

Property Value

Type: string

A string that specifies the text contents of this text element.

This property is read/write. The default value is an empty string.

Managed Equivalent

Run.Text or TextBlock.Text

Remarks

The Text property applies to two objects: Run and TextBlock. The object model overlaps here somewhat.

The TextBlock object is the primary text element for displaying text in Silverlight-based applications. A TextBlock can be thought of as having two simultaneous object models within it:

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

  • A representation as an Inlines collection. The collection contains primarily Run objects, each of which can declare its own formatting properties such as FontSize. The Text property still returns a value (the appended text of all Run elements in the Inlines collection) but does not capture any formatting that is applied to the Run elements. If created from XAML as inner text of a <TextBlock> tag or by setting the Text property, the Inlines collection contains a single Run that contains that text.

You can choose to work with either object model. However, you should be aware that if you adjust the text by appending to the Text value, when the text is actually a series of text elements with individual formatting in an Inlines collection, you will flatten the previous Inlines collection content and replace it with a single new unformatted Run with your new text. This generally is not desired behavior.

If certain properties are left unset, the TextBlock text display uses the following default values:

  • The default value of the FontSize property of the rendered TextBlock is 14.666 pixels, which is exactly 11 points.

  • The default value of the FontFamily property of the rendered TextBlock is "Portable User Interface". Enabling the default font does not require any font files, either on the hosting Web server or residing in the same directory as the XAML content.

When you set text in a TextBlock, it is not necessary to explicitly specify the Text property in XAML; you can simply place text within the TextBlock container as its content or inner text, as shown in the following example.

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <TextBlock>Hello, world!</TextBlock>
</Canvas>
NoteNote:

Any leading or trailing white space is not preserved when setting the Text property.

Example

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

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <TextBlock Text="Hello, world!" />
</Canvas>

Applies To

Run

TextBlock