Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ItemsControl Class
 Items Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ItemsControl..::.Items Property

Gets the collection used to generate the content of the ItemsControl.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
<BindableAttribute(True)> _
Public ReadOnly Property Items As ItemCollection
Visual Basic (Usage)
Dim instance As ItemsControl
Dim value As ItemCollection

value = instance.Items
C#
[BindableAttribute(true)]
public ItemCollection Items { get; }
Visual C++
[BindableAttribute(true)]
public:
property ItemCollection^ Items {
    ItemCollection^ get ();
}
JScript
public function get Items () : ItemCollection
XAML Content Element Usage
<object>
  OneOrMoreElements
</object>

XAML Values

OneOrMoreElements

One or more UIElement objects.

Property Value

Type: System.Windows.Controls..::.ItemCollection
The collection that is used to generate the content of the ItemsControl. The default is an empty collection.

Content Model: This property may be used to add items to an ItemsControl. Adding a child to an ItemsControl object implicitly adds it to the ItemCollection for the ItemsControl object.

NoteNote:

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 Items 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 Items collection is made read-only and fixed-size.

When ItemsSource is in use, setting the ItemsSource property to nullNothingnullptra null reference (Nothing in Visual Basic) removes the collection and restores usage to Items, which will be an empty ItemCollection.

The Items property can contain items such as strings, objects, or other elements. The following example shows how to use the Items property to add content to a Menu control.

C#
Menu myMenu = new Menu();

TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "My Text";
myMenu.Items.Add(myTextBlock);

Button myButton = new Button();
myButton.Content = "My Button";
myMenu.Items.Add(myButton);

Note that the ItemCollection is a view, so you can use the view-related functionalities such as sorting, filtering, and grouping.

For example, if you have an instance of a ListBox, myListBox, you can do the following to sort the content of the ListBox. In this example, Content is the name of the property to sort by.

C#
myListBox.Items.SortDescriptions.Add(
    new SortDescription("Content", ListSortDirection.Descending));

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:

C#
CollectionView myView;


...


myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);

Alternately, you can specify filtering, sorting, and grouping criteria in XAML or code by using a CollectionViewSource.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker