Text Property (TextElement)
Gets or sets the text contents of the text element.
<object Text="string" .../>
value = object.Text
object.Text = value
A string specifying the text contents of this text element.
This property is read/write. The default value is an empty string.
- A representation as a Text property, which represents only string content (no formatting other than the formatting 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) but does not capture any formatting applied to the Run elements. If created from XAML as inner text of a <TextBlock> tag, or by setting to 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 a desired behavior.
There are several interesting items to note about text display if certain properties are left unset:
- 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 "Lucida Sans Unicode, Lucida Grande".
- No font files on the hosting Web server, or residing in the same directory location as the XAML content, are required to enable the default font.
When setting text in a TextBlock, it is not necessary in XAML to explicitly specify the Text property; you can use the intuitive markup syntax of placing text within the TextBlock container as its content, or inner text:
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <TextBlock>Hello, world!</TextBlock> </Canvas>
Note Any leading or trailing whitespace is not preserved when setting the Text property.
For more information on text and fonts, see Text and Fonts Overview.
The following XAML example shows how to define a TextBlock element and set its Text property to a character string:
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <TextBlock Text="Hello, world!" /> </Canvas>
The following illustration displays the result of the previous XAML content example.