Compartir a través de


Inlines (TextBlock)

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

Gets the collection of inline text elements within a TextBlock.

<object ...>
  oneOrMoreInlineElements
</object>
value = object.Inlines

XAML Values

Value

Description

oneOrMoreInlineElements

  • One or more Run or LineBreak elements interspersed, to represent the desired text representation. See Remarks.

Property Value

Type: Inlines

A collection that holds all inline text elements within the TextBlock.

This property is read-only. The default value is an empty collection.

Managed Equivalent

Inlines

Remarks

You can add inline text elements such as Run or LineBreak to the collection, by first creating the element with CreateFromXaml, and then passing the return value to Inlines.Add or Inlines.Insert. Unlike the initial XAML markup, you cannot simply add a string. You must create a new Run that has string content in order to add text to an existing Inlines collection.

You can also remove elements from the existing collection by using Inlines.Remove or Inlines.RemoveAt.

You can access elements in the existing collection with Inlines.GetItem, or clear the entire collection with Inlines.Clear.

You can think of a TextBlock as having two simultaneous object models within it:

  • A representation as a Text (TextElement) 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> object element, or by setting to 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 a desired behavior.

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, instead of being preserved as true inner text of TextBlock if you were to examine the output in an XML Document Object Model (DOM). Because of this behavior, you can freely mix inner text and inline elements; inner text can appear before any inlines or after them, and multiple blocks of inner text can be mixed with multiple inlines.

The Inlines collection is implicitly created by parsing the TextBlock content. TextBlock.Inlines as a property element is marginally permitted in XAML. The marginal aspect is because even XML white space is interpreted as possible content of the TextBlock, and if any content is detected, the parser will populate the implicit Inlines collection by using that content as an initial Run. Defining TextBlock.Inlines thereafter is thus interpreted as an attempt to set Inlines twice. Therefore, the only way to specify TextBlock.Inlines would be if no white space existed 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 Inlines object element. Instead, you generally include one or more inline types (Run and LineBreak elements) as child elements of a TextBlock. In addition to the implicit collection syntax of Inlines, TextBlock also supports setting content as inner text, or by specifically setting the value of Text with a string. For details, see TextBlock.

Applies To

TextBlock

See Also

Reference