Gets the collection of inline text elements within a TextBlock.
value = object.Inlines
A collection that holds all inline text elements within the TextBlock.
This property is read-only. The default value is an empty collection.
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.
A TextBlock can be thought of as having two simultaneous object models within it:
- 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.
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 TextBlock if you were to examine the output in an XML 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 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 there were was no white space at all between the <TextBlock> object element and <TextBlock.Inlines> object 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. For more information about XAML implicit collection syntax, see XAML Syntax Overview. 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.