共用方式為


TextBlock.Inlines Property

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

Gets the collection of inline text elements within a TextBlock.

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

Syntax

'Declaration
Public ReadOnly Property Inlines As InlineCollection
public InlineCollection Inlines { get; }
<TextBlock
  oneOrMoreInlineElements
</TextBlock>

XAML Values

  • oneOrMoreInlineElements
    One or more object elements for classes that derive from Inline. Typically these are the Silverlight classes Run or LineBreak.

Property Value

Type: System.Windows.Documents.InlineCollection
A collection that holds all inline text elements from the TextBlock.
The default is an empty collection.

Remarks

You can remove elements from the existing collection by using the Remove or RemoveAt method of the InlineCollection.

You can access elements in the existing collection with the Item property. You can clear the entire collection with by calling the Clear method.

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.

A TextBlock can contain mixed inner text and inline elements. In this case, each of the inner text sections is parsed and converted into a Run, rather than being preserved as true inner text of the TextBlock. Because of this behavior, you can freely mix inner text and child elements, which is not typically permitted in XAML. Inner text of a TextBlock can appear before or after any child elements of a TextBlock. Multiple blocks of inner text can be mixed with multiple child elements.

The InlineCollection is created implicitly by parsing the TextBlock content. Setting TextBlock.Inlines as an explicit property element is permitted in XAML. However, this is not recommended, because even XML white space is interpreted as possible content of the TextBlock. If any content is detected, the parser populates the implicit InlineCollection by using that content as an initial Run. (Because setting the property is not recommended, property element syntax is not illustrated in the syntax section.) If you define TextBlock.Inlines after setting the property, it is interpreted as an attempt to set Inlines twice. The only way to specify TextBlock.Inlines as a property element would be if there were no white space at all between the TextBlock object element and TextBlock.Inlines property element.

The XAML syntax for Inlines is an example of an implicit collection syntax, where you omit an actual InlineCollection object element. Instead, you include one or more inline types (Run and LineBreak elements) as child elements of a TextBlock. In addition to the implicit collection syntax of inline elements, TextBlock also lets you specify content as inner text, or by setting the value of Text by using a string. For more information, see the TextBlock class description.

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.