#77 – XAML Collection Syntax
September 27, 2010 6 Comments
When setting a property value in XAML for a property that represents some sort of collection, you can directly specify the children of the collection as a series of elements.
For example, to create some entries in a ListBox, you can do the following:
<ListBox Height="100" HorizontalAlignment="Left" Margin="64,66,0,0" Name="listBox1" VerticalAlignment="Top" Width="120"> <ListBox.Items> <ListBoxItem Content="Oak"/> <ListBoxItem Content="Red Pine"/> <ListBoxItem Content="Larch"/> </ListBox.Items> </ListBox>
The ListBox.Items property is of type ItemCollection, so you might expect to see the tag <ItemCollection> under the property element <ListBox.Items>. But because the Items property is a collection, you can just list the child elements of the collection directly.
May want to cover collection properties which are r/o vs. collection/dictionary properties which are r/w.
FrameworkElement.Resources and Panel.Children.
You may never mention UIElementCollection in XAML. You may mention ResourceDictionary in XAML…in which case the constructor will be called instead of the getter of the .Resources property…
Thanks, Rob
Thanks Rob, I’ll investigate a bit deeper and then expand on this. I appreciate the attention.
Rob, just to clarify.. Is it the case that:
If the collection-based property is read-only (e.g. Panel.Children), you can never include an object element in XAML because you’d be asking to create a new instance of the property type. Instead, XAML parser calls the getter to get the already-created instance, then the .Add method of underlying collection class to add the child elements.
If collection-based property is read/write (e.g. FrameworkElement.Resources), you can create a new instance of the collection–which is what happens when you do include XAML object element–you create new instance and then call setter to point property to your new instance. And then you call .Add to add the child elements.
Is that what you meant? That you can never specify collection property explicitly if it’s ro, but only if r/w?
Thanks,
Sean
Yep. And if the first element of a R/W collection property is assignable to that property value, that is when we assume that we don’t need to call the getter…
One more point of clarification–when is a new instance of the collection object created? E.g. for FrameworkElement.Resources, if you do include object element for and then child elements for each resource, I’m assuming that you always create new instance of the ResourceDictionary and then assign it to Resources property. But if there is already an instance of the collection object in existence after construction of the parent, and you omit the object element, do you then just call getter to get the existing collection? If you omit the object element for the collection and the property is still null, do you then call constructor? I.e. Does construction vs. get depend on omitting the object element, or just on existence of an instance?
Pingback: #83 – Collection Syntax for Read-Only Vs. Read-Write Properties « 2,000 Things You Should Know About WPF