Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/how-to-create-and-bind-to-an-observablecollection.md
T
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

4.9 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create and Bind to an ObservableCollection Find out how to create and bind to a collection that derives from the ObservableCollection class in Windows Presentation Foundation. 03/30/2017
csharp
vb
data binding [WPF], ObservableCollection class
notifications [WPF]
6cf7e275-df76-41c6-a611-53b889b8fd5a

How to: Create and Bind to an ObservableCollection

This example shows how to create and bind to a collection that derives from the xref:System.Collections.ObjectModel.ObservableCollection%601 class, which is a collection class that provides notifications when items get added or removed.

Example

The following example shows the implementation of a NameList collection:

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; }  
      }  
  }  
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  

You can make the collection available for binding the same way you would with other common language runtime (CLR) objects, as described in Make Data Available for Binding in XAML. For example, you can instantiate the collection in [!INCLUDETLA2#tla_xaml] and specify the collection as a resource, as shown here:

<Window  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  xmlns:x="http://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>  

You can then bind to the collection:

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

The definition of NameItemTemplate is not shown here.

Note

The objects in your collection must satisfy the requirements described in the Binding Sources Overview. In particular, if you are using xref:System.Windows.Data.BindingMode.OneWay or xref:System.Windows.Data.BindingMode.TwoWay (for example, you want your [!INCLUDETLA2#tla_ui] to update when the source properties change dynamically), you must implement a suitable property changed notification mechanism such as the xref:System.ComponentModel.INotifyPropertyChanged interface.

For more information, see the Binding to Collections section in the Data Binding Overview.

See also