Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. Advanced Data Binding > Building a change-aware collection type

Building a change-aware collection type

Building a change-aware collection type Applies to Silverlight 3, 4 and 5 We may not always have the option of binding to a collection that implements the INotifyCollectionChanged interface. For example, what if we have a service that returns IList<T>? Can't we use the automatic synchronization features that Silverlight's data binding engine offers us? The good news is that we can. For that, we need to build a wrapper class around the IList<T>. This class will implement the necessary interface and will allow data binding to work in the manner in which we are accustomed. Getting ready The complete solution for this recipe can be found in the Chapter03/CustomCollections folder in the code bundle available on the Packt website. How to do it... For this recipe, we'll assume that we need to work with an external assembly called UnchangeableCode in the sample code, which we simply can't change. Inside the assembly, a class returns a list of Owner instances as IList<Owner>. However, in our Silverlight application, we would still like to use the synchronization that data binding offers us. We'll implement this by building a wrapper class. We need to perform the following steps in order to achieve this: The UnchangeableCode project contains a class called OwnerService. This class contains a List<Owner> as shown in the following code: public class OwnerService { private List<Owner> owners; public List<Owner> Owners { get { return owners; } set { owners = value; } } public OwnerService() { owners = new List<Owner>(); Owner o1 = new Owner() { Name = "Gill Cleeren", CurrentBalance = 100 }; Owner o2 = new Owner() { Name = "Kevin Dockx", CurrentBalance = 200 }; Owner o3 = new Owner() { Name = "Marina Smith", CurrentBalance = 300 }; Owner o4 = new Owner() { Name = "Lindsey Smith", CurrentBalance = 400 }; owners.Add(o1); owners.Add(o2); owners.Add(o3); owners.Add(o4); } } In our Silverlight application, we would like to bind to the list of Owner instances not only for displaying the data, but also for viewing any changes done to the list immediately. We'll create a class that wraps around the List<Owner>. This class will also implement the INotifyCollectionChanged interface as shown in the following code: public class CustomOwnerList : IList<Owner>, INotifyCollectionChanged { private IList<Owner> owners; public CustomOwnerList(IList<Owner> owner) { this.owners = owner; } } We can now start implementing all the methods that are defined by both interfaces. The INotifyCollectionChanged interface defines only one event, which is called the CollectionChanged event. This is shown in the following line of code: public event NotifyCollectionChangedEventHandler CollectionChanged; The IList interface contains a lot of methods that we need to implement. The following is the code for the Insert method. Notice that we're manually calling the CollectionChanged event when something changes in the list. We wrap the call of the CollectionChanged event in the OnCollectionChanged method. This method includes checking that the event isn't null. The other methods are similar and the code for these methods can be found in the code bundle available on the Packt website. public void Insert(int index, Owner item) { owners.Insert(index, item); OnCollectionChanged(new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index)); } private void OnCollectionChanged(NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { if (CollectionChanged != null) CollectionChanged(this,notifyCollectionChangedEventArgs); } Now that we have the wrapper, we can work with the list as if it's a regular ObservableCollection. Whenever we add, remove, or change items in the list, we'll see those changes directly in the UI. The following code shows the instantiation of the new collection and sets it as the DataContext for a ListBox control: OwnerService someOldClass = new OwnerService(); CustomOwnerList list = new CustomOwnerList(someOldClass.Owners); OwnerListBox.ItemsSource = list; How it works... If we want to make use of the automatic synchronization offered by Silverlight's data binding for a collection, then this collection should implement the INotifyCollectionChanged interface. If it doesn't do this, we can still bind and show the items in the collection. However, changes to the collection won't be propagated into the UI. Although using the ObservableCollection is advised, sometimes we need to work with a service or an assembly from a third party that returns, for example, a generic list. If we want the data of the generic list to be bound to the UI and the changes to the list to be visualized, then we need to build a class that wraps around the list. This class needs to implement the IList<T> interface. As a result, while implementing the methods, we work with the original list itself. For example, while implementing the Insert method, we insert an item in a specific location in the underlying list. Also, the class needs to implement the INotifyCollectionChanged interface. For every change that is done in the list (such as adding an item), our wrapper class will raise the CollectionChanged event. Now, whenever we want to bind, we bind to an instance of our wrapper class. Silverlight notices that this class implements the INotifyCollectionChanged interface, so it will register for the events that are raised by an instance of the wrapper class. See also Binding to regular collections is explained in the Binding collections to UI elements recipe of the previous chapter.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint