次の方法で共有


方法: 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 の最上位のインライン要素を取得する

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

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

Span 内の最後の Inline 要素を削除する

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

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

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

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

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

こちらも参照ください