#77 – XAML Collection Syntax

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">
         <ListBoxItem Content="Oak"/>
         <ListBoxItem Content="Red Pine"/>
         <ListBoxItem Content="Larch"/>

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.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

6 Responses to #77 – XAML Collection Syntax

  1. Rob Relyea says:

    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

    • Sean says:

      Thanks Rob, I’ll investigate a bit deeper and then expand on this. I appreciate the attention.

    • Sean says:

      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?


  2. Rob Relyea says:

    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…

    • Sean says:

      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?

  3. Pingback: #83 – Collection Syntax for Read-Only Vs. Read-Write Properties « 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: