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

プロパティ値

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

次の例では、オブジェクトItemsControlItemsSourceバインドします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

次の例では、プロパティを使用して設定する ItemsControl 方法を Items 示します。 この例では、次のさまざまな種類の項目を次のように 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 例で作成した内容を示しています。

4 種類の内容を含む ListBox

ItemCollectionこれはビューであるため、並べ替え、フィルター処理、グループ化などのビュー関連の機能を使用できます。

たとえば、 のインスタンスListBoxmyListBoxがある場合は、次の操作を実行してListBox、 . この例では、 Content 並べ替えるプロパティの名前を指定します。

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

この操作を行うと、コントロールがコレクションに直接バインドされている場合、既定のコレクション ビューが使用され、並べ替え条件が同じコレクションに直接バインドされている他のすべてのコントロールに適用されることに注意してください。 プロパティが にバインドされているCollectionViewSource場合、ビューは既定のItemsSourceビューになりません。

ItemsControlコレクションに直接バインドされている場合は、次の操作を行って既定のビューを取得できます。

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

または、XAML またはコードでフィルター処理、並べ替え、グループ化の条件を CollectionViewSource指定することもできます。

注釈

このプロパティは、アイテム ItemsControlを追加するために使用できます。 オブジェクトに子を追加すると、 ItemsControl 暗黙的にオブジェクトの ItemCollection 子に ItemsControl 追加されます。

注意

このプロパティは、表示されているコレクション構文を使用するか、コレクション オブジェクトにアクセスし、次のような Addさまざまなメソッドを使用して、拡張アプリケーション マークアップ言語 (XAML) でのみ設定できます。 コレクション オブジェクト自体にアクセスするプロパティは読み取り専用で、コレクション自体は読み取り/書き込みです。

またはプロパティを使用 Items して、コンテンツの ItemsSource 生成に使用するコレクションを ItemsControl指定します。 プロパティを ItemsSource 設定すると、 Items コレクションは読み取り専用で固定サイズになります。

使用中の場合 ItemsSource は、プロパティを ItemsSource 設定してコレクションを null 削除し、使用状況を Items復元します。これは空 ItemCollectionになります。

XAML プロパティ要素の使用

<object>  
  OneOrMoreElements  
</object>  

XAML 値

OneOrMoreElements
1 つ以上の UIElement オブジェクト。

適用対象

こちらもご覧ください