WPF - 引用多个资源
专家们,
在 XAML 中,我想在实体之间创建多对多关系。
基本上我希望多个“管理器”对象能够管理多个“项目”。以下 XAML 应该描述我要查找的内容:
<Grid>
<Grid.Resources>
<cc:Manager x:Key="Manager1"/>
<cc:Manager x:Key="Manager2"/>
</Grid.Resources>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
</cc.Manager.ManagedBy>
</cc:Item>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" /> <!-- ERROR HERE -->
</cc.Manager.ManagedBy>
</cc:Item>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager2" />
</cc.Manager.ManagedBy>
</cc:Item>
</Grid>
附加属性 (Manager.ManagedBy) 的类型为 ManagedByCollection
...
ManagedByCollection : List<ManageBy>
这样,我收到以下错误消息:
The object 'Object' already has a child and cannot add 'StaticResourceExtension'. 'Object' can accept only one child. Line NN Position NN.
So, I wen't back查阅 MSDN,发现有一个 ContentPropertyAttribute 可以告诉 XAML 编译器,当没有指定其他属性时,哪个属性是默认属性。例如,LinearGradientBrush 使用该属性使我们能够只编写 ...
<LinearGradientBrush ... >
<GradientStop ... />
<GradientStop ... />
<GradientStop ... />
</LinearGradientBrush>
... 而不是 ...
<LinearGradientBrush ... >
<GradientStopCollection>
<GradientStop ... />
<GradientStop ... />
<GradientStop ... />
</GradientStopCollection>
</LinearGradientBrush>
所以,我想我只需要将 ManagedByCollection
的索引器指定为类' ContentProperty:
[ContentProperty("Item")
ManagerCollection : List<Manager>
不幸的是,这并不能解决问题。目前以下工作...
<cc.Manager.ManagedBy>
<ManagerCollection>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" />
<cc:ManagerCollection>
</cc.Manager.ManagedBy>
...但是,我更喜欢更易读的语法:
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" />
</cc.Manager.ManagedBy>
任何帮助或提示将不胜感激。
Experts,
In XAML I would like to create a many-to-many relationship between entities.
Basically I would like for multiple "Manager" objects to be able to manage multiple "Items". The following XAML should describe what I'm looking for:
<Grid>
<Grid.Resources>
<cc:Manager x:Key="Manager1"/>
<cc:Manager x:Key="Manager2"/>
</Grid.Resources>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
</cc.Manager.ManagedBy>
</cc:Item>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" /> <!-- ERROR HERE -->
</cc.Manager.ManagedBy>
</cc:Item>
<cc:Item>
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager2" />
</cc.Manager.ManagedBy>
</cc:Item>
</Grid>
The attached property (Manager.ManagedBy) is of type ManagedByCollection
...
ManagedByCollection : List<ManageBy>
With this I get the following error message:
The object 'Object' already has a child and cannot add 'StaticResourceExtension'. 'Object' can accept only one child. Line NN Position NN.
So, I wen't back to MSDN and realized there's a ContentPropertyAttribute to tell the XAML compiler what property is the default one when nothing else is specified. The LinearGradientBrush, for example, uses that attribute to enable us to write just ...
<LinearGradientBrush ... >
<GradientStop ... />
<GradientStop ... />
<GradientStop ... />
</LinearGradientBrush>
... instead of ...
<LinearGradientBrush ... >
<GradientStopCollection>
<GradientStop ... />
<GradientStop ... />
<GradientStop ... />
</GradientStopCollection>
</LinearGradientBrush>
So, I was thinking I just needed to specify the indexer of ManagedByCollection
as the class' ContentProperty:
[ContentProperty("Item")
ManagerCollection : List<Manager>
Unfortunately, this does not resolve the issue. Currently the following works...
<cc.Manager.ManagedBy>
<ManagerCollection>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" />
<cc:ManagerCollection>
</cc.Manager.ManagedBy>
... but, again, I would prefer the more readble syntax:
<cc.Manager.ManagedBy>
<StaticResource ResourceKey="Manager1" />
<StaticResource ResourceKey="Manager2" />
</cc.Manager.ManagedBy>
Any help or hints would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
Item
的构造函数中显式初始化集合:You can initialize the collection explicitly in the constructor of
Item
: