다음을 통해 공유


방법: ObservableCollection 만들기 및 바인딩

이 예제에서는 항목이 추가되거나 제거될 때 알림을 제공하는 컬렉션 클래스인 ObservableCollection<T> 클래스에서 파생된 컬렉션을 만들고 이 컬렉션에 바인딩하는 방법을 보여 줍니다.

예제

다음 예제에서는 NameList 컬렉션의 구현을 보여 줍니다.

Public Class NameList
    Inherits ObservableCollection(Of PersonName)

    ' Methods
    Public Sub New()
        MyBase.Add(New PersonName("Willa", "Cather"))
        MyBase.Add(New PersonName("Isak", "Dinesen"))
        MyBase.Add(New PersonName("Victor", "Hugo"))
        MyBase.Add(New PersonName("Jules", "Verne"))
    End Sub

End Class

Public Class PersonName
    ' Methods
    Public Sub New(ByVal first As String, ByVal last As String)
        Me._firstName = first
        Me._lastName = last
    End Sub


    ' Properties
    Public Property FirstName() As String
        Get
            Return Me._firstName
        End Get
        Set(ByVal value As String)
            Me._firstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return Me._lastName
        End Get
        Set(ByVal value As String)
            Me._lastName = value
        End Set
    End Property


    ' Fields
    Private _firstName As String
    Private _lastName As String
End Class
public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("Willa", "Cather"));
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }

방법: XAML의 바인딩에 사용할 수 있는 데이터 만들기에 설명되어 있는 것처럼 다른 common language runtime (CLR) 개체의 경우와 마찬가지 방식으로 해당 컬렉션을 바인딩에 사용할 수 있게 만들 수 있습니다. 예를 들어 다음과 같이 XAML에서 컬렉션을 인스턴스화하고 컬렉션을 리소스로 지정할 수 있습니다.


<Window
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:SDKSample"
  x:Class="SDKSample.Window1"
  Width="400"
  Height="280"
  Title="MultiBinding Sample">
    
  <Window.Resources>
    <c:NameList x:Key="NameListData"/>


...


</Window.Resources>

그런 다음 컬렉션에 바인딩할 수 있습니다.

<ListBox Width="200"
         ItemsSource="{Binding Source={StaticResource NameListData}}"
         ItemTemplate="{StaticResource NameItemTemplate}"
         IsSynchronizedWithCurrentItem="True"/>

NameItemTemplate의 정의는 여기에 나와 있지 않습니다.

참고참고

컬렉션의 개체는 바인딩 소스 개요에 설명된 요구 사항에 맞아야 합니다.특히 OneWay 또는 TwoWay를 사용하는 경우(예: 소스 속성이 동적으로 변경될 때 UI 업데이트) INotifyPropertyChanged 인터페이스와 같은 적절한 속성 변경 알림 메커니즘을 구현해야 합니다.

자세한 내용은 데이터 바인딩 개요에서 "컬렉션에 바인딩" 단원을 참조하십시오.

참고 항목

작업

방법: 뷰의 데이터 정렬

방법: 뷰에서 데이터 필터링

방법: XAML에서 뷰를 사용하여 데이터 정렬 및 그룹화

개념

데이터 바인딩 개요

기타 리소스

데이터 바인딩 방법 항목