WPF:无法在XAML中设置只读集合的​​项目 [英] WPF: Cannot set items of a read-only collection in XAML

查看:84
本文介绍了WPF:无法在XAML中设置只读集合的​​项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种情况非常简单。我有一个自定义控件的只读collection属性,我想在XAML中设置集合的项。像这样:

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>

ControlItems 属性的内部集公共获取访问器,类型为 FreezableCollection< CustomItem>

The ControlItems property has internal set and public get accessors, and is of type FreezableCollection<CustomItem>.

问题是我遇到构建错误,说这是不可能的,因为我的 ControlItems 属性没有一个可访问的集合访问器。

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.

据我所知,这种情况在WPF中受支持(从.NET3.5SP1开始) )。我错了吗?可能是什么问题?这适用于 Grid.RowDefinitions ,我尝试添加 DesignerSerializationVisibility 属性,但是它不起作用。

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.

编辑:我注意到只有当我有一个内部私有受保护的集访问器。当我完全删除访问器后,一切正常。

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.

推荐答案

好的,让我在这里写点东西以将其标记为已回答。如果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.

这篇关于WPF:无法在XAML中设置只读集合的​​项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆