Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/textelement-content-model-overview.md
T
Andy De George 99e90fa162 Update metadata rules and individual articles (#142)
* Add conceptual topic type for framework

* Add NET WPF article topic types

* Framework winforms overview topic types

* Update metadata

* Update walkthrough-my-first-wpf-desktop-application.md
2020-12-03 09:58:46 -08:00

10 KiB

title, ms.date, ms.topic, dev_langs, helpviewer_keywords, ms.assetid
title ms.date ms.topic dev_langs helpviewer_keywords ms.assetid
TextElement Content Model Overview 03/30/2017 overview
csharp
vb
documents [WPF], flow documents
TextElement content model [WPF]
flow content elements [WPF], TextElement content model
d0a7791c-b090-438c-812f-b9d009d83ee9

TextElement Content Model Overview

This content model overview describes the supported content for a xref:System.Windows.Documents.TextElement. The xref:System.Windows.Documents.Paragraph class is a type of xref:System.Windows.Documents.TextElement. A content model describes what objects/elements can be contained in others. This overview summarizes the content model used for objects derived from xref:System.Windows.Documents.TextElement. For more information, see Flow Document Overview.

Content Model Diagram

The following diagram summarizes the content model for classes derived from xref:System.Windows.Documents.TextElement as well as how other non- TextElement classes fit into this model.

Diagram: Flow content containment schema

As can be seen from the preceding diagram, the children allowed for an element are not necessarily determined by whether a class is derived from the xref:System.Windows.Documents.Block class or an xref:System.Windows.Documents.Inline class. For example, a xref:System.Windows.Documents.Span (an xref:System.Windows.Documents.Inline-derived class) can only have xref:System.Windows.Documents.Inline child elements, but a xref:System.Windows.Documents.Figure (also an xref:System.Windows.Documents.Inline-derived class) can only have xref:System.Windows.Documents.Block child elements. Therefore, a diagram is useful for quickly determining what element can be contained in another. As an example, let's use the diagram to determine how to construct the flow content of a xref:System.Windows.Controls.RichTextBox.

  1. A xref:System.Windows.Controls.RichTextBox must contain a xref:System.Windows.Documents.FlowDocument which in turn must contain a xref:System.Windows.Documents.Block-derived object. The following is the corresponding segment from the preceding diagram.

    Diagram: RichTextBox containment rules

    Thus far, this is what the markup might look like.

    [!code-xamlFlowOvwSnippets_snip#SchemaWalkThrough1]

  2. According to the diagram, there are several xref:System.Windows.Documents.Block elements to choose from including xref:System.Windows.Documents.Paragraph, xref:System.Windows.Documents.Section, xref:System.Windows.Documents.Table, xref:System.Windows.Documents.List, and xref:System.Windows.Documents.BlockUIContainer (see Block-derived classes in the preceding diagram). Let's say we want a xref:System.Windows.Documents.Table. According to the preceding diagram, a xref:System.Windows.Documents.Table contains a xref:System.Windows.Documents.TableRowGroup containing xref:System.Windows.Documents.TableRow elements, which contain xref:System.Windows.Documents.TableCell elements which contain a xref:System.Windows.Documents.Block-derived object. The following is the corresponding segment for xref:System.Windows.Documents.Table taken from the preceding diagram.

    Diagram: Parent/child schema for Table

    The following is the corresponding markup.

    [!code-xamlFlowOvwSnippets_snip#SchemaWalkThrough2]

  3. Again, one or more xref:System.Windows.Documents.Block elements are required underneath a xref:System.Windows.Documents.TableCell. To make it simple, let's place some text inside the cell. We can do this using a xref:System.Windows.Documents.Paragraph with a xref:System.Windows.Documents.Run element. The following is the corresponding segments from the diagram showing that a xref:System.Windows.Documents.Paragraph can take an xref:System.Windows.Documents.Inline element and that a xref:System.Windows.Documents.Run (an xref:System.Windows.Documents.Inline element) can only take plain text.

    Diagram: Parent/child schema for Paragraph

    Diagram: Parent/Child schema for Run

The following is the entire example in markup.

[!code-xamlFlowOvwSnippets_snip#SchemaExampleWholePage]

Working with TextElement Content Programmatically

The contents of a xref:System.Windows.Documents.TextElement is made up by collections and so programmatically manipulating the contents of xref:System.Windows.Documents.TextElement objects is done by working with these collections. There are three different collections used by xref:System.Windows.Documents.TextElement -derived classes:

You can manipulate (add or remove items) from these collections using the respective properties of Inlines, Blocks, and ListItems. The following examples show how to manipulate the contents of a Span using the Inlines property.

Note

Table uses several collections to manipulate its contents, but they are not covered here. For more information, see Table Overview.

The following example creates a new xref:System.Windows.Documents.Span object, and then uses the Add method to add two text runs as content children of the xref:System.Windows.Documents.Span.

[!code-csharpSpanSnippets#_SpanInlinesAdd] [!code-vbSpanSnippets#_SpanInlinesAdd]

The following example creates a new xref:System.Windows.Documents.Run element and inserts it at the beginning of the xref:System.Windows.Documents.Span.

[!code-csharpSpanSnippets#_SpanInlinesInsert] [!code-vbSpanSnippets#_SpanInlinesInsert]

The following example deletes the last xref:System.Windows.Documents.Inline element in the xref:System.Windows.Documents.Span.

[!code-csharpSpanSnippets#_SpanInlinesRemoveLast] [!code-vbSpanSnippets#_SpanInlinesRemoveLast]

The following example clears all of the contents (xref:System.Windows.Documents.Inline elements) from the xref:System.Windows.Documents.Span.

[!code-csharpSpanSnippets#_SpanInlinesClear] [!code-vbSpanSnippets#_SpanInlinesClear]

Types That Share This Content Model

The following types inherit from the xref:System.Windows.Documents.TextElement class and may be used to display the content described in this overview.

xref:System.Windows.Documents.Bold, xref:System.Windows.Documents.Figure, xref:System.Windows.Documents.Floater, xref:System.Windows.Documents.Hyperlink, xref:System.Windows.Documents.InlineUIContainer, xref:System.Windows.Documents.Italic, xref:System.Windows.Documents.LineBreak, xref:System.Windows.Documents.List, xref:System.Windows.Documents.ListItem, xref:System.Windows.Documents.Paragraph, xref:System.Windows.Documents.Run, xref:System.Windows.Documents.Section, xref:System.Windows.Documents.Span, xref:System.Windows.Documents.Table, xref:System.Windows.Documents.Underline.

Note that this list only includes nonabstract types distributed with the Windows SDK. You may use other types that inherit from xref:System.Windows.Documents.TextElement.

Types That Can Contain TextElement Objects

See WPF Content Model.

See also