ItemsControl.Items 属性


获取用于生成 ItemsControl 的内容的集合。Gets the collection used to generate the content of the ItemsControl.

 property System::Windows::Controls::ItemCollection ^ Items { System::Windows::Controls::ItemCollection ^ get(); };
public System.Windows.Controls.ItemCollection Items { get; }
member this.Items : System.Windows.Controls.ItemCollection
Public ReadOnly Property Items As ItemCollection


用于生成 ItemsControl 的内容的集合。The collection that is used to generate the content of the ItemsControl. 默认值为空集合。The default is an empty collection.



下面的示例演示如何将数据绑定ItemsControl到。The following examples demonstrate binding data to an ItemsControl. 第一个示例创建一个名MyData为的类,该类是一个简单的字符串集合。The first example creates a class called MyData that is a simple string collection.

public class MyData : ObservableCollection<string>
    public MyData()
        Add("Item 1");
        Add("Item 2");
        Add("Item 3");
Public Class MyData
    Inherits ObservableCollection(Of String)

    Public Sub New()  '

        Add("Item 1")
        Add("Item 2")
        Add("Item 3")

    End Sub
End Class

下面的示例将的ItemsSource对象ItemsControl绑定到MyDataThe following example binds the ItemsSource object of an ItemsControl to MyData.

<!--Create an instance of MyData as a resource.-->
<src:MyData x:Key="dataList"/>
<ListBox ItemsSource="{Binding Source={StaticResource dataList}}"/>
ListBox listBox1 = new ListBox();
MyData listData = new MyData();
Binding binding1 = new Binding();

binding1.Source = listData;
listBox1.SetBinding(ListBox.ItemsSourceProperty, binding1);
Dim listBox1 As New ListBox()
Dim listData As New MyData()
Dim binding1 As New Binding()

binding1.Source = listData
listBox1.SetBinding(ListBox.ItemsSourceProperty, binding1)

下图显示了在ListBox上一个示例中创建的控件。The following illustration shows the ListBox control created in the previous example.


下面的示例演示如何ItemsControl Items使用属性填充。The following example demonstrates how to populate an ItemsControl by using the Items property. 该示例将以下不同类型的项添加到中ListBoxThe example adds the following different types of items to the ListBox:

<!--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"

  <!-- 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>

  <TextBlock>String in a TextBlock</TextBlock>
// Add a String to the ListBox.
listBox1.Items.Add("This is a string in a ListBox");

// Add a DateTime object to a ListBox.
DateTime dateTime1 = new DateTime(2004, 3, 4, 13, 6, 55);


// Add a Rectangle to the ListBox.
Rectangle rect1 = new Rectangle();
rect1.Width = 40;
rect1.Height = 40;
rect1.Fill = Brushes.Blue;

// Add a panel that contains multpile objects to the ListBox.
Ellipse ellipse1 = new Ellipse();
TextBlock textBlock1 = new TextBlock();

ellipse1.Width = 40;
ellipse1.Height = 40;
ellipse1.Fill = Brushes.Blue;

textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.Text = "Text below an Ellipse";


' 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)


' 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

' 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"



下图显示了在ListBox上一个示例中创建的。The following illustration shows the ListBox created in the previous example.

包含四种内容类型的 ListBoxListBox with four types of content

请注意,是一个视图,因此可以使用与视图相关的功能,如排序、筛选和分组。 ItemCollectionNote that the ItemCollection is a view, so you can use the view-related functionalities such as sorting, filtering, and grouping.

例如,如果你具有的实例ListBoxmyListBox则可以执行以下操作以对的内容ListBox进行排序。For example, if you have an instance of a ListBox, myListBox, you can do the following to sort the content of the ListBox. 在此示例中Content ,是要按其排序的属性的名称。In this example, Content is the name of the property to sort by.

    new SortDescription("Content", ListSortDirection.Descending));
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. 如果该ItemsSource属性绑定CollectionViewSource到,则不会将该视图作为默认视图。The view will not be the default view if the ItemsSource property is bound to a CollectionViewSource.

ItemsControl如果直接绑定到集合,则可以执行以下操作来获取默认视图:If your ItemsControl is bound directly to a collection, then you can do the following to get the default view:

CollectionView myView;
Private myView As CollectionView
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)

或者,您可以XAMLXAML CollectionViewSource使用指定筛选、排序和分组条件或代码。Alternately, you can specify filtering, sorting, and grouping criteria in XAMLXAML or code by using a CollectionViewSource.


此属性可用于向添加项ItemsControlThis property may be used to add items to an ItemsControl. 如果将子ItemsControl对象添加到对象中,则会将其ItemsControl隐式添加到对象的ItemCollectionAdding a child to an ItemsControl object implicitly adds it to the ItemCollection for the ItemsControl object.


此属性只能可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)通过显示的集合语法来设置,或者通过访问集合对象并使用它的各种方法Add来设置。This property can only be set in 可扩展应用程序标记语言 (XAML)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.

请注意,使用Items ItemsSource或属性来指定应用于ItemsControl生成的内容的集合。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. 如果设置ItemsSource了属性Items ,则集合将变为只读且固定大小。When the ItemsSource property is set, the Items collection is made read-only and fixed-size.

使用ItemsSource时, ItemsSource将属性设置为可null删除集合,并将使用情况还原Items为空。 ItemCollectionWhen ItemsSource is in use, setting the ItemsSource property to null removes the collection and restores usage to Items, which will be an empty ItemCollection.

XAML 属性元素用法XAML Property Element Usage



一个或多UIElement个对象。One or more UIElement objects.