新しいコレクション プロパティを継承しSystem.Windows.Documents.Paragraph
て追加するクラスを作成しています。以下は、そのクラスの非常に単純化された表現です。
public class ExtendedParagraph : Paragraph
{
public Dictionary<string, string> Attributes { get; set; }
}
Xaml 内から上記のクラスのインスタンスを作成して設定する必要があります。これには、段落のコンテンツとその Attributes コレクションのメンバーを個別に宣言できるマークアップ構文が必要です。
Paragraph クラスは attributeで修飾されて[ContentProperty("Inlines")]
いるため、Inlines コレクションと Attributes コレクションを明示的に設定する必要があると思います。他の場所で同様の課題を解決するために使用されている Xaml 構文に基づいて、次のようなものを想定しています。
<ExtendedParagraph xmlns="clr-namespace:MyNamespace">
<ExtendedParagraph.Inlines>
This is where the paragraph content goes
</ExtendedParagraph.Inlines>
<ExtendedParagraph.Attributes>
This is where the members of the Attributes property are declared
</ExtendedParagraph.Attributes>
</ExtendedParagraph>
ただし、このアプローチには 2 つの問題があります。
[1] XamlReader を使用して上記の Xaml を解析すると、「ExtendedParagraph.Inlines プロパティは既に設定されており、一度だけ設定できます」というメッセージが表示されて失敗します。
[2] Attributes 要素内で KeyValuePair のインスタンスを宣言するためにどのマークアップを使用すればよいかわかりません。
誰かが私を正しい方向に向けてくれることを願っています。
どうもありがとう、ティム
編集 - 質問 [1] に対する回答が見つかりました。最初に (プロパティ要素の構文を使用して) Attributes コレクションを宣言し、その後に Paragraph のコンテンツを宣言するだけです。
<ExtendedParagraph xmlns="clr-namespace:MyNamespace">
<ExtendedParagraph.Attributes>
This is where the members of the Attributes property are declared
</ExtendedParagraph.Attributes>
This is where the paragraph content goes
</ExtendedParagraph>
ただし、メンバーを a に宣言的に追加することは、Dictionary<TKey, TValue>
より困難であることがわかっています。この投稿でいくつかの手がかりを見つけましたが、まだ実用的な結果を達成していません。あなたのアイデアは大歓迎です。