[Silverlight 2, C#] Implementing an Observable Collection [on a custom class]

In order to improve some performance issues we were having, I needed to make a custom class that we were using become observable so that when we changed an item in the class the datagrid that the class was bound to would update automatically.

The datasource we were using was a custom class of ours, so I therefore had to make it be observable as a collection to allow for this auto updating notification to occur.

Here is how you setup your class to enable it to be part of an observable collection.

// 1: Implement the INotifyPropertyChanged interface on your class.
public class MyCustomClass : INotifyPropertyChanged
{
  // 2: Setup member variables; they must implement get;set.
  private string _myProp;
  public string MyProp
  {
    get { return _myProp; }
    set
    {
       // Check to make sure the value is actually changing.
       // [Thank you to Aaron for pointing this out]
       if (_myProp != value)
       {
         _myProp = value;
         // 3: Call the function which will notify subscribers that a property has changed.
         NotifyPropertyChanged("MyProp");
       }
    }
  } 

  //...  More member variables if you wish

  // 4: Setup the PropertyChangedEventHandler event on your class which will be responsible for telling subscribers that a property has changed; this is required by the INotifyPropertyChanged interface.
  public event PropertyChangedEventHandler PropertyChanged;

  // 5: Define the function [which is called in all your set functions], which is responsible for firing the PropertyChanged event on your class.  Must check for PropertyChanged != null, to make sure that someone is listening before firing the event.
  private void NotifyPropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs((propertyName)));
    }
  }

  // ...  The rest of your class.
}

// 6: Now you can create observable collections of your class, which can then be bound as data sources for controls.
public class MyCustomCollection: ObservableCollection<MyCustomClass> { }

Then you can use that class as the itemsource [making sure that there are bindings in the XAML for the properties in MyCustomClass] for controls.

Links:
Silverlight.net forum post
MSDN – INotifyPropertyChanged Interface



Tags: ,
This entry was posted on Monday, May 25th, 2009 at 11:55 am and is filed under Silverlight, c#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “[Silverlight 2, C#] Implementing an Observable Collection [on a custom class]”

  1. Aaron

    You’d usually want to check to see that the value in your property setter has changed before calling the NotifyPropertyChanged method/event.

  2. Jack Bond

    One minor observation, you probably don’t want the NotifyPropertyChanged method to be public.

  3. admin

    Good call Mr. Bond ;)

Leave a Reply

Your comment