方法: Inlines プロパティを介してフロー コンテンツ要素を操作する

この例では、Inlines プロパティを介して、インライン フロー コンテンツ要素 (および TextBlock などの、そのような要素のコンテナー) に対して実行できる一般的な操作をいくつか示します。 このプロパティは、InlineCollection の項目を追加および削除するために使用されます。 Inlines プロパティを使用するフロー コンテンツ要素には、次のようなものがあります。

この例では、フロー コンテンツ要素として Span が使用されることがありますが、これらの手法は、InlineCollection コレクションをホストするすべての要素またはコントロールに適用できます。

新しい Span オブジェクトを作成する

次の例では、新しい Span オブジェクトを作成し、Add メソッドを使用して、Span のコンテンツの子として 2 つのテキスト実行を追加します。

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 要素を作成する

次の例では、新しい 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 の最上位のインライン要素を取得する

次の例では、Span に含まれる最上位レベルの Inline 要素の数を取得します。

int countTopLevelInlines = spanx.Inlines.Count;
Dim countTopLevelInlines As Integer = spanx.Inlines.Count

Span の最後のインライン要素を削除する

次の例では、Span 内の最後の Inline 要素を削除します。

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

Span からインライン要素のすべてのコンテンツをクリアする

次の例では、Span からすべてのコンテンツ (Inline 要素) をクリアします。

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

関連項目