我正在創建一個繼承自System.Windows.Documents.Paragraph
的類並添加了一個新的集合屬性。下面是類的一個非常簡化的表示:如何填充Xaml元素中的多個集合屬性
public class ExtendedParagraph : Paragraph
{
public Dictionary<string, string> Attributes { get; set; }
}
我需要創建並從內XAML中,這需要一個標記語法,其允許該段的內容和成員填充上述類的實例其屬性集合需要單獨聲明。
由於段落類用屬性[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>
然而,這種方法存在兩個問題:
[1]當上述XAML是使用解析XamlReader,它失敗,消息「ExtendedParagraph.Inlines屬性已被設置,並且只能設置一次」
[2]我不知道我應該用什麼標記來聲明KeyValuePair在Attributes元素中的實例。
我希望有人能指出我正確的方向。
非常感謝, 添
編輯 - 我已經找到了答案質疑[1]。它僅僅需要聲明屬性集合(使用屬性元素語法)第一,其次是該段的內容:
<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>
然而,聲明性地將成員添加到一個Dictionary<TKey, TValue>
被證明更加困難。我在this post找到了一些線索,但是我還沒有取得一個工作成果。你的想法仍然受歡迎。