WPF:无法在 XAML 中设置只读集合的项目
场景非常简单。我的自定义控件有一个只读集合属性,并且我想在 XAML 中设置集合的项目。像这样:
<l:CustomControl>
<l:CustomControl.ControlItems>
<l:CustomItem />
<l:CustomItem />
</l:CustomControl.ControlItems>
</l:CustomControl>
ControlItems
属性具有 internal set
和 public get
访问器,并且类型为 FreezableCollection
。
问题是我收到构建错误,表明这是不可能的,因为我的 ControlItems
属性没有可访问的 set
访问器。
据我所知,WPF(从.NET3.5SP1开始)支持这种情况。我错了吗?可能是什么问题?这适用于 Grid.RowDefinitions,我尝试添加 DesignerSerializationVisibility 属性,但它不起作用。
编辑:我注意到仅当我有 internal
、private
或 protected
设置访问器时才会收到此错误。当我完全删除访问器时,一切都构建得很好。
The scenario is really simple. I have a read-only collection property of my custom control, and I want set the items of the collection in XAML. Like this:
<l:CustomControl>
<l:CustomControl.ControlItems>
<l:CustomItem />
<l:CustomItem />
</l:CustomControl.ControlItems>
</l:CustomControl>
The ControlItems
property has internal set
and public get
accessors, and is of type FreezableCollection<CustomItem>
.
The thing is that I am getting build errors that say this is not possible because my ControlItems
property does not have an accessable set
accessor.
As I know, this scenario is supported in WPF (as of .NET3.5SP1). Am I wrong? What might be the problem? This works with Grid.RowDefinitions
, I tried adding the DesignerSerializationVisibility
attribute but it did not work.
Edit: I noticed that I receive this error only when I have an internal
, private
or protected
set accessor. When I completely remove the accessor, everything builds fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,让我在这里写一些东西来将其标记为已回答。如果 XAML 解析器确实具有内部、私有或受保护的集方法,则它无法将项添加到控件中。解决方案是删除内部/私有/受保护集方法。如果不存在“set”方法,则一切正常。这听起来很愚蠢,但这就是事实。
我的一位队友也遇到了这个问题,所以我猜大家一定也遇到了同样的问题。
OK, let me write something here to mark this as answered. XAML parser cannot add items to your control if it does have an internal, private, or protected set method. The solution is to remove the internal/private/protected set method. If no 'set' method exists, everything is fine. This sounds stupid, but this is the fact.
This issue also did hit one of my teammates, so I guess people must be hitting the same issue.