假设我对只读 Items
有一些控制权收藏。我可以使用集合语法初始化这个集合:
<Something>
<Something.Items>
<Item />
<Item />
</Something.Items>
</Something>
假设现在我在资源中有集合并且想用它初始化我的控件:
<Window.Resources>
<ItemCollectionClass x:Key="collection">
<Item />
<Item />
</ItemCollectionClass>
</Window.Resources>
怎么做? <Something Items="{StaticResource collection}" />
不起作用,因为它试图设置集合实例,而不是初始化它。
最佳答案
您可以使用 ObjectDataProvider 初始化集合:
<Window.Resources>
<ItemCollectionClass x:Key="collection">
<Item />
<Item />
</ItemCollectionClass>
<ObjectDataProvider x:Key="..." ObjectType="{x:Type local:Something}">
<ObjectDataProvider.ConstructorParameters>
<StaticResource ResourceKey="collection" />
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</Window.Resources />
关于c# - 如何使用 XAML 中的资源初始化只读项集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6776867/