如何:通过 Blocks 属性操作 FlowDocument
以下示例演示通过 Blocks 属性可以对 FlowDocument 执行的部分较常见的操作。
示例
下面的示例创建一个新的 FlowDocument,然后向 FlowDocument 中追加新的 Paragraph 元素。
Dim flowDoc As New FlowDocument(New Paragraph(New Run("A bit of text content...")))
flowDoc.Blocks.Add(New Paragraph(New Run("Text to append...")))
FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run("A bit of text content...")));
flowDoc.Blocks.Add(new Paragraph(new Run("Text to append...")));
下面的示例创建一个新的 Paragraph 元素并将其插入到 FlowDocument 的开始位置。
Dim p As New Paragraph(New Run("Text to insert..."))
flowDoc.Blocks.InsertBefore(flowDoc.Blocks.FirstBlock, p)
Paragraph p = new Paragraph(new Run("Text to insert..."));
flowDoc.Blocks.InsertBefore(flowDoc.Blocks.FirstBlock, p);
下面的示例获取包含在 FlowDocument 中的顶级 Block 元素的数目。
Dim countTopLevelBlocks As Integer = flowDoc.Blocks.Count
int countTopLevelBlocks = flowDoc.Blocks.Count;
下面的示例删除 FlowDocument 中的最后一个 Block 元素。
flowDoc.Blocks.Remove(flowDoc.Blocks.LastBlock)
flowDoc.Blocks.Remove(flowDoc.Blocks.LastBlock);
下面的示例从 FlowDocument 中清除所有内容(Block 元素)。
flowDoc.Blocks.Clear()
flowDoc.Blocks.Clear();