If I have a simple object:
class MyObject
{
public string MyValueProperty { get; set; }
}
and if I want to instantiate it in XAML and set the property (as below), this works:
<local:MyObject MyValueProperty="SomeValue" />
However, if my object has a collection property on it:
class MyObject
{
public MyObject() { this.MyCollectionProperty = new List<string>(); }
public IList<string> MyCollectionProperty { get; set; }
}
then I cannot work out how to add items to it through XAML. What I would like to be able to do is something like:
<local:MyObject>
<local:MyObject.MyCollectionProperty>
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
<sys:String>etc</sys:String>
</local:MyObject.MyCollectionProperty>
</local:MyObject>
I have tried this but I get a parse exception stating that I can’t set String to a property of type IList<string>, and I realise I could probably work around this by adding an instantiation of a `List’ to the markup, but I want to avoid this.
Any suggestions?
Worked it out – the problem was because my property was defined as an
IList<T>and so the XAML parser couldn’t work out how to instantiate an appropriate instance (even though it already had an instance specified.Changing the property declaration to
List<string>get it working: