Gets the collection used to generate the content of the ItemsControl.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
'Declaration <BindableAttribute(True)> _ Public ReadOnly Property Items As ItemCollection
This property can only be set in Extensible Application Markup Language (XAML) via the collection syntax shown, or by accessing the collection object and using its various methods such as Add. The property to access the collection object itself is read-only, and the collection itself is read-write.
Note that you use either the or the ItemsSource property to specify the collection that should be used to generate the content of your ItemsControl. When the ItemsSource property is set, the collection is made read-only and fixed-size.
The following examples demonstrate binding data to an ItemsControl. The first example creates a class called MyData that is a simple string collection.
The following illustration shows the ListBox control created in the previous example.
A DateTime object.
A UIElement object.
<!--Create a ListBox that contains a string, a Rectangle, a Panel, and a DateTime object. These items can be accessed via the Items property.--> <ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib" Name="simpleListBox"> <!-- The <ListBox.Items> element is implicitly used.--> This is a string in a ListBox <sys:DateTime>2004/3/4 13:6:55</sys:DateTime> <Rectangle Height="40" Width="40" Fill="Blue"/> <StackPanel Name="itemToSelect"> <Ellipse Height="40" Fill="Blue"/> <TextBlock>Text below an Ellipse</TextBlock> </StackPanel> <TextBlock>String in a TextBlock</TextBlock> <!--</ListBox.Items>--> </ListBox>
' Create a Button with a string as its content. listBox1.Items.Add("This is a string in a ListBox") ' Create a Button with a DateTime object as its content. Dim dateTime1 As New DateTime(2004, 3, 4, 13, 6, 55) listBox1.Items.Add(dateTime1) ' Create a Button with a single UIElement as its content. Dim rect1 As New Rectangle() rect1.Width = 40 rect1.Height = 40 rect1.Fill = Brushes.Blue listBox1.Items.Add(rect1) ' Create a Button with a panel that contains multiple objects ' as its content. Dim ellipse1 As New Ellipse() Dim textBlock1 As New TextBlock() ellipse1.Width = 40 ellipse1.Height = 40 ellipse1.Fill = Brushes.Blue textBlock1.TextAlignment = TextAlignment.Center textBlock1.Text = "Text below an Ellipse" stackPanel1.Children.Add(ellipse1) stackPanel1.Children.Add(textBlock1) listBox1.Items.Add(stackPanel1)
The following illustration shows the ListBox created in the previous example.
Note that the ItemCollection is a view, so you can use the view-related functionalities such as sorting, filtering, and grouping.
Note that when you do this, if the control is bound to a collection directly, the default collection view is used, and the sort criteria are applied to all other controls bound to the same collection directly. The view will not be the default view if the ItemsSource property is bound to a CollectionViewSource.
If your ItemsControl is bound directly to a collection, then you can do the following to get the default view:
Alternately, you can specify filtering, sorting, and grouping criteria in XAML or code by using a CollectionViewSource.