ItemsControl.ItemsSource Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets or sets a collection used to generate the content of the ItemsControl.
Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
<itemsControl ItemsSource="bindingDeclaration"/> -or- <itemsControl ItemsSource="resourceReferenceToIEnumerable"/>
XAML Values
Property Value
Type: System.Collections.IEnumerableThe object that is used to generate the content of the ItemsControl. The default is a null reference (Nothing in Visual Basic).
Dependency property identifier field: ItemsSourceProperty
You can add items to an ItemsControl by adding items to the Items property or by setting the ItemsSource property to a data collection. You can add objects of different types to Items. If ItemsSource is not a null reference (Nothing in Visual Basic), the items in the items property are read-only. You cannot add an object or change the objects in the Items property.
You should set the ItemsSource to an object that implements the INotifyCollectionChanged interface so that changes in the collection will be reflected in the ItemsControl. The ObservableCollection(T) class defines such an object.
The following example creates a ListBox, which inherits from ItemsControl, and binds it to a collection of Customer objects. The example sets the DisplayMemberPathProperty to the LastName property of the customer. Therefore, the ListBox displays the following values:
Anderberg
Ashton
Hicks
Pica
<Grid> <Grid.Resources> <src:Customers x:Key="customers"/> </Grid.Resources> <ListBox ItemsSource="{StaticResource customers}" Width="250" Margin="0,5,0,10" DisplayMemberPath="LastName" /> </Grid>
The following example shows the Customer class and the collection that the ListBox is bound to.
The following example creates a ListBox and sets the ItemsSource property to a collection of strings. For this example, src is defined as xmlns:src="clr-namespace:ItemsControlSnippets".
<StackPanel Grid.Column="1"> <StackPanel.Resources> <src:Items x:Key="items"/> </StackPanel.Resources> <ListBox Name="lb2" ItemsSource="{Binding Source={StaticResource items}}"/> <TextBlock Name="tb2"/> </StackPanel>
The following example iterates through each string in the Items property of the ListBox in the previous example and adds each item to the TextBlock, tb2. The Items property contains the collection of strings that the ListBox is bound to. You can get the strings by using the Items property, but you cannot add or change the strings.
The following example shows the collection of strings that the ListBox is bound to.