如何:通过 Inlines 属性操作流内容元素
更新:2007 年 11 月
这些示例演示可通过 Inlines 属性在内联流内容元素(以及这些元素的容器,例如 TextBlock)上执行的一些常见操作。此属性用于从 InlineCollection 添加和移除项。具有 Inlines 属性的流内容元素包括:
这些示例只是将 Span 用作流内容元素,但是这些技术可应用于承载 InlineCollection 集合的所有元素或控件。
示例
下面的示例创建一个新 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..."));
下面的示例创建一个新 Run 元素并将其插入到 Span 的开始位置。
Run runx = new Run("Text to insert...");
spanx.Inlines.InsertBefore(spanx.Inlines.FirstInline, runx);
下面的示例获取包含在 Span 中的顶级 Inline 元素的数目。
int countTopLevelInlines = spanx.Inlines.Count;
下面的示例删除 Span 中的最后一个 Inline 元素。
spanx.Inlines.Remove(spanx.Inlines.LastInline);
下面的示例从 Span 中清除所有内容(Inline 元素)。
spanx.Inlines.Clear();
请参见
任务
如何:通过 Blocks 属性操作 FlowDocument