Internal MailBag: Collection Types in XAML
Another mail that mail be useful to others outside our firewall…
By the way, MSDN has a section that goes into Collection Types and XAML Collection Properties
From: Mike
Btw, the reason List<T> works is because it implements IList.
—
From: Rob
IList, IDictionary or Array are the collection types we support.
List<T> or IList<T> is recommended as well, as it suggests the type of the children.
IList<T> isn’t supported by itself…partially due to dev time and partially due to perf concerns…if I recall correctly.
—
From: Developer
I’m designing a class that I want to be able to use in XAML. The class which has a collection-valued property called “Children”.
If I define “Children” as IList<T>, I get the following error: ‘Children’ is a read-only IEnumerable property, so ‘MyClass’ must implement IAddChild.
If I define “Children” as List<T>, things seem to work fine.
Can someone clarify what types XAML supports for collections? Why is IList<T> not acceptable? What is the recommended usage here?
I\’ve seen a similar but strange issue pop up when using a HierarchicalDataTemplate. If I attempt to bind the ItemsSource to a property named Children (that implements IList<T>) then I get an error. If I rename the property to Kids, it works fine.
In my opinion the easiest to use for this purpose are:
System.Collections.ObjectModel.Collection and
System.Collections.KeyedCollection (great for named items)