WPF: Cannot set items of a read-only collection in 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>
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 access开发者_如何学JAVAor, everything builds fine.
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.
精彩评论