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

다음 예제에서는 to의 ItemsSource 개체를 ItemsControl 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

다음 예제에서는 속성을 사용하여 Items 채우는 ItemsControl 방법을 보여 줍니다. 이 예제에서는 다음과 같은 다양한 유형의 항목을 다음 항목에 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 줍니다.

네 가지 형식의 콘텐츠가 있는 ListBox

ItemCollection 보기이므로 정렬, 필터링 및 그룹화와 같은 보기 관련 기능을 사용할 수 있습니다.

예를 들어 인스턴스가 있는 ListBoxmyListBox경우 다음을 수행하여 내용을 ListBox정렬할 수 있습니다. 이 예제에서는 Content 정렬 기준으로 사용할 속성의 이름입니다.

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

이렇게 하면 컨트롤이 컬렉션에 직접 바인딩된 경우 기본 컬렉션 뷰가 사용되고 정렬 조건이 동일한 컬렉션에 직접 바인딩된 다른 모든 컨트롤에 적용됩니다. 속성이 .에 바인딩된 경우 ItemsSource 뷰는 기본 보기가 CollectionViewSource아닙니다.

ItemsControl 컬렉션에 직접 바인딩된 경우 다음을 수행하여 기본 보기를 가져올 수 있습니다.

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

또는 을 사용하여 CollectionViewSourceXAML 또는 코드에서 필터링, 정렬 및 그룹화 조건을 지정할 수 있습니다.

설명

이 속성은 항목을 ItemsControl추가하는 데 사용할 수 있습니다. 개체에 자식 개체를 ItemsControl 추가하면 개체에 대한 ItemsControl 자식이 ItemCollection 암시적으로 추가됩니다.

참고

이 속성은 표시된 컬렉션 구문을 통해서만 XAML(Extensible Application Markup Language)에서 설정하거나 컬렉션 개체에 액세스하고 다양한 메서드 Add를 사용하여 설정할 수 있습니다. 컬렉션 개체 자체에 액세스하는 속성은 읽기 전용이며 컬렉션 자체는 읽기/쓰기가 가능합니다.

속성 또는 Items ItemsSource 속성을 사용하여 콘텐츠를 생성하는 데 사용해야 하는 컬렉션을 지정합니다 ItemsControl. 속성이 ItemsSource 설정되면 컬렉션이 Items 읽기 전용이고 고정 크기로 설정됩니다.

사용 중인 경우 ItemsSource 컬렉션을 제거하도록 null 속성을 설정 ItemsSource 하면 사용량이 비어 ItemCollection있는 상태로 Items복원됩니다.

XAML 속성 요소 사용

<object>  
  OneOrMoreElements  
</object>  

XAML 값

OneOrMoreElements
하나 이상의 UIElement 개체입니다.

적용 대상

추가 정보