#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">
     <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.

Advertisements