#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

Advertisement

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

3 Responses to #969 – Items Property of ListBox Contains List of Items

  1. Pingback: Dew Drop – December 11, 2013 (#1681) | Morning Dew

  2. Pingback: #970 – Avoid Working Directly with Items Collection | 2,000 Things You Should Know About WPF

  3. Pingback: #971 – Items Property is a Content Property | 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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: