TextElement 内容模型概述

本内容模型概述描述了 TextElement 支持的内容。 Paragraph 类是 TextElement 的类型。 内容模型描述哪些对象/元素可以包含在其他对象/元素中。 本概述汇总了派生自 TextElement 的对象所使用的内容模型。 有关更多信息,请参见流文档概述

本主题包括下列各节。

  • 内容模型关系图
  • 以编程方式处理 TextElement 内容
  • 共享此内容模型的类型
  • 可包含 TextElement 对象的类型
  • 相关主题

内容模型关系图

下面的关系图对派生自 TextElement 的类所使用的内容模型,以及其他非 TextElement 类如何适应该模型进行了汇总。

示意图:流内容包含架构

如上面的关系图所示,元素可以具有的子元素不一定通过某个类派生自 Block 类还是 Inline 类来确定。 例如,Span(从 Inline 派生的类)只能具有 Inline 子元素,但是 Figure(也是从 Inline 派生的类)只能具有 Block 子元素。 因此,关系图可用于快速地确定哪些元素可以包含在其他元素中。 例如,我们可以使用关系图来确定如何构造 RichTextBox 的流内容。

  1. 一个 RichTextBox 必须包含一个 FlowDocument,而后者又必须包含一个派生自 Block 的对象。 下图是上述关系图中的相应一部分。

    示意图:RichTextBox 包含规则

    到此为止,标记可能类似于所示内容。

    <RichTextBox>
      <FlowDocument>
        <!-- One or more Block-derived object… -->
      </FlowDocument>
    </RichTextBox>
    
  2. 按照该关系图,存在多个可以从中进行选择的 Block 元素,包括 ParagraphSectionTableListBlockUIContainer(请参见上图中从 Block 派生的类)。 假设我们需要一个 Table。 按照上面的关系图,一个 Table 包含一个 TableRowGroup,后者包含多个 TableRow 元素,这些行元素又包含多个 TableCell 元素,而这些单元格元素又包含一个从 Block 派生的对象。 下图是本文档内第一个关系图中与 Table 相对应的一部分。

    示意图:表的父/子架构

    下面是相应的标记。

    <RichTextBox>
      <FlowDocument>
        <Table>
          <TableRowGroup>
            <TableRow>
              <TableCell>
                <!-- One or more Block-derived object… -->
              </TableCell>
            </TableRow>
          </TableRowGroup>
        </Table>
      </FlowDocument>
    </RichTextBox>
    
  3. 同样,TableCell 下需要一个或多个 Block 元素。 为简单起见,我们在单元格内部放置一些文本。 我们可以使用一个带有 Run 元素的 Paragraph 来实现该操作。 下图是本文档内第一个关系图中的一部分,由该图可见,Paragraph 可以包含一个 Inline 元素,而 Run(一个 Inline 元素)只能包含纯文本。

    示意图:段落的父/子架构

     

    示意图:运行的父/子架构

下面是标记中的完整示例。

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
  <RichTextBox>
    <FlowDocument>

      <!-- Normally a table would have multiple rows and multiple
           cells but this code is for demonstration purposes.-->
      <Table>
        <TableRowGroup>
          <TableRow>
            <TableCell>
              <Paragraph>

                <!-- The schema does not actually require
                     explicit use of the Run tag in markup. It 
                     is only included here for clarity. -->
                <Run>Paragraph in a Table Cell.</Run>
              </Paragraph>
            </TableCell>
          </TableRow>
        </TableRowGroup>
      </Table>

    </FlowDocument>
  </RichTextBox>
</Page>

以编程方式处理 TextElement 内容

TextElement 的内容由集合组成,因此,可以通过处理这些集合来以编程方式操作 TextElement 对象的内容。 从 TextElement 派生的类可以使用以下三个不同的集合:

可以从这些集合中,分别使用 InlineBlockListItem 的属性来进行操作(添加或移除项)。 下面的示例演示如何使用 Inline 属性来操作 Span 的内容。

注意注意

Table 使用多个集合来操作其内容,但是这里我们不对其进行说明。有关更多信息,请参见表概述

下面的示例创建一个新 Span 对象,然后使用 Add 方法添加两个文本运行,作为 Span 的子内容。

            Dim spanx As New Span()
            spanx.Inlines.Add(New Run("A bit of text content..."))
            spanx.Inlines.Add(New Run("A bit more text content..."))
Span spanx = new Span();
spanx.Inlines.Add(new Run("A bit of text content..."));
spanx.Inlines.Add(new Run("A bit more text content..."));

下面的示例创建一个新 Run 元素并将其插入到 Span 的开始位置。

            Dim runx As New Run("Text to insert...")
            spanx.Inlines.InsertBefore(spanx.Inlines.FirstInline, runx)
Run runx = new Run("Text to insert...");
spanx.Inlines.InsertBefore(spanx.Inlines.FirstInline, runx);

下面的示例删除 Span 中的最后一个 Inline 元素。

            spanx.Inlines.Remove(spanx.Inlines.LastInline)
spanx.Inlines.Remove(spanx.Inlines.LastInline);

下面的示例从 Span 中清除所有内容(Inline 元素)。

            spanx.Inlines.Clear()
spanx.Inlines.Clear();

共享此内容模型的类型

下面的类型继承自 TextElement 类,可以用来显示本概述中描述的内容。

Bold, Figure, Floater, Hyperlink, InlineUIContainer, Italic, LineBreak, List, ListItem, Paragraph, Run, Section, Span, Table, Underline.

请注意,此列表中仅包括与 Windows SDK 一起分发的非抽象类型。 可以使用继承自 TextElement 的其他类型。

可包含 TextElement 对象的类型

请参见 WPF 内容模型

请参见

任务

如何:通过 Blocks 属性操作 FlowDocument

如何:通过 Blocks 属性操作流内容元素

如何:通过 Blocks 属性操作 FlowDocument

如何:通过 Columns 属性操作表列

如何:通过 RowGroups 属性操作表的行组