ItemsControl.Items 屬性

定義

取得用來產生 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

屬性值

用來產生 ItemsControl 之內容的集合。 預設為空集合。

屬性

範例

下列範例示範將資料系結至 ItemsControl 。 第一個範例會建立稱為 的 MyData 類別,該類別是簡單的字串集合。

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

下列範例會將 ItemsSourceItemsControl 物件系結至 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 上一個範例中建立的控制項。

ListBox ListBox

下列範例示範如何使用 屬性填入 ItemsControlItems 。 此範例會將下列不同類型的專案新增至 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 上一個範例中建立的 。

具有四種內容類型

請注意, ItemCollection 是檢視,因此您可以使用檢視相關的功能,例如排序、篩選和分組。

例如,如果您有 的 ListBox 實例, myListBox 您可以執行下列動作來排序 的內容 ListBox 。 在此範例中, Content 是排序依據的屬性名稱。

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

請注意,當您這樣做時,如果控制項直接系結至集合,則會使用預設集合檢視,而且排序準則會套用至系結至相同集合的所有其他控制項。 如果屬性系結至 CollectionViewSourceItemsSource 則檢視不會是預設檢視。

ItemsControl如果您直接系結至集合,您可以執行下列動作以取得預設檢視:

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

或者,您也可以使用 CollectionViewSource ,在 XAML 或程式碼中指定篩選、排序和分組準則。

備註

這個屬性可用來將專案加入至 ItemsControl 。 將子系加入至 ItemsControl 物件,會隱含地將它新增至 ItemCollection 物件的 ItemsControl

注意

此屬性只能在 Extensible Application Markup Language 中設定, (XAML) 透過顯示的集合語法,或是存取集合物件,並使用其各種方法,例如 Add 。 存取集合物件本身的屬性是唯讀的,而且集合本身是讀寫的。

請注意,您可以使用 ItemsItemsSource 屬性來指定應該用來產生 內容的 ItemsControl 集合。 ItemsSource設定 屬性時, Items 集合會變成隻讀且固定大小。

當 正在使用時 ItemsSource ,將 屬性設定 ItemsSourcenull 會移除集合,並將使用方式還原為 Items ,這會是空 ItemCollection 的 。

XAML 屬性項目用法

<object>  
  OneOrMoreElements  
</object>  

XAML 值

OneOrMoreElements
一或多個 UIElement 物件。

適用於

另請參閱