Export (0) Print
Expand All

ItemsControl.Items Property

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

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[BindableAttribute(true)] 
public ItemCollection Items { get; }
/** @property */
public ItemCollection get_Items ()

public function get Items () : ItemCollection

<object>
  oneOrMoreElements
</object>
 oneOrMoreElements  One or more UIElement objects. 

Property Value

The collection that is used to generate the content of this 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. For more information on the content model for ItemsControl, see Items Control Content Model.

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, 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 will be made read-only and fixed-size.

When ItemsSource is in use, setting the ItemsSource property to a 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.

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, and therefore, 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.

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

Note that when you do this, the view may or may not be the default view, depending on how the data is set up on your ItemsControl. For example, when the ItemsSource property is bound to a CollectionViewSource, the view that you obtain using the Items property is not the default view.

If your ItemsControl is bound (you are using the ItemsSource property), then you can do the following to get the default view:

    CollectionView myView;

...

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

Alternately, you can specify filtering, sorting, and grouping criteria in XAML using CollectionViewSource.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft