TextElement 内容模型概述
此内容模型概述描述了 TextElement支持的内容。 Paragraph 类是一种 TextElement类型。 内容模型描述哪些对象/元素可以包含在其他对象中。 本概述总结了用于派生自 TextElement的对象的内容模型。 有关详细信息,请参阅 流文档概述。
内容模型关系图
下图汇总了派生自 TextElement 的类的内容模型,以及其他非 TextElement
类如何适应此模型。
如上图所示,某个元素允许的子级不一定由该类是否从 Block 类或 Inline 类派生来决定。 例如,Span(Inline派生类)只能有 Inline 子元素,但 Figure(也是 Inline派生类)只能有 Block 子元素。 因此,关系图可用于快速确定可包含在另一个元素中的元素。 例如,让我们使用示意图来确定如何构建 RichTextBox的流程内容。
RichTextBox 必须包含 FlowDocument,而后者又必须包含 Block 派生的对象。 下面是上图中的相应段。
到此为止,标记可能类似于所示内容。
<RichTextBox> <FlowDocument> <!-- One or more Block-derived object… --> </FlowDocument> </RichTextBox>
根据关系图,有几个 Block 元素可供选择,包括 Paragraph、Section、Table、List和 BlockUIContainer(请参阅上图中的块派生类)。 假设需要一个 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>
同样,在 TableCell下需要存在一个或多个 Block 元素。 为简单起见,在单元格内部放置一些文本。 可以使用 Paragraph 与 Run 元素来执行此操作。 下图中的相应段显示 Paragraph 可以采用 Inline 元素,并且 Run(Inline 元素)只能采用纯文本。
的父/子架构
的父/子架构
下面是标记中的完整示例。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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 -derived 类使用了三个不同的集合:
InlineCollection:表示 Inline 元素的集合。 InlineCollection 定义 Paragraph、Span和 TextBlock 元素的允许子内容。
BlockCollection:表示 Block 元素的集合。 BlockCollection 定义 FlowDocument、Section、ListItem、TableCell、Floater和 Figure 元素的允许子内容。
ListItemCollection:表示有序或无序 List中特定内容项的流内容元素。
可以使用 内联、块和 列表项的相应属性操作这些集合中的项(添加或删除项)。 以下示例演示如何使用 Inlines 属性操作 Span 的内容。
说明
表使用多个集合来操作其内容,但此处未介绍它们。 有关详细信息,请参阅 表概述。
以下示例创建新的 Span 对象,然后使用 Add
方法将两个文本运行添加为 Span 的内容子级。
Span spanx = new Span();
spanx.Inlines.Add(new Run("A bit of text content..."));
spanx.Inlines.Add(new Run("A bit more text content..."));
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..."))
以下示例创建一个新的 Run 元素,并将其插入到 Span的开头。
Run runx = new Run("Text to insert...");
spanx.Inlines.InsertBefore(spanx.Inlines.FirstInline, runx);
Dim runx As New Run("Text to insert...")
spanx.Inlines.InsertBefore(spanx.Inlines.FirstInline, runx)
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 内容模型。