#971 – Items Property is a Content Property

Every control that derives from ItemsControl (including the ListBox) has an Items property that stores the collection of items represented by the control.

The Items property of an ItemsControl is also its content property.  This means that you can set the value of the Items property directly in XAML by specifying a list of child elements for the control that derives from ItemsControl.

For example, you can explicitly specify the child elements using property element syntax:

        <ListBox Margin="15" Width="250" Height="250">
            <ListBox.Items>
                <ListBoxItem Content="Thing 1"/>
                <ListBoxItem Content="Thing 2"/>
            </ListBox.Items>
        </ListBox>

Because the Items property is a content property, you can be more concise, doing the following:

        <ListBox Margin="15" Width="250" Height="250">
            <ListBoxItem Content="Thing 1"/>
            <ListBoxItem Content="Thing 2"/>
        </ListBox>
Advertisements

#970 – Avoid Working Directly with Items Collection

When you use a ListBox (or any other ItemsControl), you’ll most often populate the control by binding the ItemsSource property of the control to a collection.  The Items property of the control can then be examined, if you want to look at the individual items in the collection.

In practice, however, you’ll work directly with the collection that the ListBox is bound to, rather than working with the Items property.  For example:

  • To add items, add them to the underlying collection
  • To remove items, remove them from the underlying collection
  • To change an item, change the corresponding item in the underlying collection

The example below adds a new Actor to a bound collection when a button is clicked.  Note that the ListBox is updated immediately.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ActorList.Add(new Actor("Marty Feldman", 1934, 1982, "Young Frankenstein"));
        }

970-001

#969 – Items Property of ListBox Contains List of Items

Deriving from an ItemsControl, a ListBox provides access to the list of items contained in the ListBox by way of its Items property.  The Items property is an ItemCollection, which supports the IList and ICollection interfaces, as well as providing properties for grouping, sorting and filtering items in the collection.

Entries in the ItemCollection can be any .NET object (derives from System.Object).  You’ll most often use data binding to bind the ItemsSource property of the ListBox to some sort of collection that supports IEnumerable.  (E.g. an ObservableCollection).  Doing this will set Items to refer to the bound collection.

You can access individual items from code using an indexer on the Items property.

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Note: Our ListBox contains instances of Actor objects
    Actor actor = lbMyListBox.Items[0] as Actor;
    if (actor != null)
        MessageBox.Show(string.Format("First Actor in list is: {0}", actor.FullName));
}

969-001