TextElement 內容模型概觀

此 con帳篷模式l 概觀描述 的支援 TextElement 內容。 類別 Paragraph 是 的型別 TextElement 。 內容模型描述哪些物件/元素可包含於其他物件/元素內。 本概觀摘要說明衍生自 TextElement 的物件所使用的 con帳篷模式l。 如需詳細資訊,請參閱 非固定格式文件概觀

內容模型圖表

下圖摘要說明衍生自 TextElement 之類別的 con帳篷模式l,以及其他非 TextElement 類別如何納入此模型。

Diagram: Flow content containment schema

如上圖所示,允許元素的子系不一定取決於類別衍生自 Block 類別或 Inline 類別。 例如, SpanInline 衍生類別)只能有子專案,但 Figure (也 Inline 衍生類別) 只能有 InlineBlock 子專案。 因此,可以快速判斷哪些項目可以包含其他項目的圖表就很有用。 例如,讓我們使用圖表來判斷如何建構 的 RichTextBox 流程內容。

  1. RichTextBox必須包含 FlowDocument ,其接著必須包含 Block 衍生的物件。 以下是上圖的對應區段。

    Diagram: RichTextBox containment rules

    這是標記目前可能的樣子。

    <RichTextBox>
      <FlowDocument>
        <!-- One or more Block-derived object… -->
      </FlowDocument>
    </RichTextBox>
    
  2. 根據圖表,有幾個 Block 元素可供選擇,包括 ParagraphSectionTableListBlockUIContainer (請參閱上圖中的區塊衍生類別)。 假設我們想要 。 Table 根據上圖,包含 TableTableRowGroup 包含的 TableRow 元素,其中包含 TableCell 包含 Block 衍生物件的專案。 以下是從上圖中取得的對應區段 Table

    Diagram: Parent/child schema for 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 ) 只能採用純文字。

    Diagram: Parent/child schema for Paragraph

    Diagram: Parent/Child schema for Run

以下是標記的完整範例。

<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 三個不同的集合:

您可以使用 InlinesBlocksListItems 的個別屬性,從這些集合中進行管理 (新增或移除項目)。 下列範例示範如何使用 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)

下列範例會刪除 中的 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 散發的非abstract 類型。 您可以使用繼承自 TextElement 的其他類型。

可包含 TextElement 物件的類型

請參閱 WPF 內容模型

另請參閱