ItemsControl.Items 屬性

定義

取得用來產生 ItemsControl 之內容的集合。Gets the collection used to generate the content of the ItemsControl.

public:
 property System::Windows::Controls::ItemCollection ^ Items { System::Windows::Controls::ItemCollection ^ get(); };
[System.ComponentModel.Bindable(true)]
public System.Windows.Controls.ItemCollection Items { get; }
[<System.ComponentModel.Bindable(true)>]
member this.Items : System.Windows.Controls.ItemCollection
Public ReadOnly Property Items As ItemCollection

屬性值

ItemCollection

用來產生 ItemsControl 之內容的集合。The collection that is used to generate the content of the ItemsControl. 預設為空集合。The default is an empty collection.

屬性

範例

下列範例示範如何將資料系結至 ItemsControlThe 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 物件系結 ItemsControlMyDataThe 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.

ListBoxListBox

下列範例示範如何 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"
         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>
// 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);

listBox1.Items.Add(dateTime1);

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

// 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";

stackPanel1.Children.Add(ellipse1);
stackPanel1.Children.Add(textBlock1);

listBox1.Items.Add(stackPanel1);
' 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)

下圖顯示在 ListBox 上一個範例中建立的。The following illustration shows the ListBox created in the previous example.

具有四種內容的 ListBoxListBox with four types of content

請注意, ItemCollection 是一個視圖,因此您可以使用與視圖相關的功能,例如排序、篩選和群組。Note that the ItemCollection is a view, so you can use the view-related functionalities such as sorting, filtering, and grouping.

例如,如果您有的實例 ListBoxmyListBox 您可以執行下列動作來排序的內容 ListBoxFor 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.

myListBox.Items.SortDescriptions.Add(
    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. 如果屬性系結至,則此視圖將不會是預設的 view ItemsSource CollectionViewSourceThe view will not be the default view if the ItemsSource property is bound to a CollectionViewSource.

如果您 ItemsControl 是直接系結至集合,則可以執行下列動作來取得預設的 view: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 CollectionViewSourceAlternately, 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 物件中時,會隱含地將它加入至 ItemCollection 物件的 ItemsControlAdding a child to an ItemsControl object implicitly adds it to the ItemCollection for the ItemsControl object.

注意

這個屬性只能透過 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 所顯示的集合語法,或藉由存取集合物件並使用其各種方法(例如)在中設定 AddThis property can only be set in Extensible Application Markup Language (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.

請注意,您可以使用 ItemsItemsSource 屬性來指定應該用來產生之內容的集合 ItemsControlNote 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

<object>  
  OneOrMoreElements  
</object>  

XAML 值XAML Values

OneOrMoreElementsOneOrMoreElements
一或多個 UIElement 物件。One or more UIElement objects.

適用於

另請參閱