Implementing the ListCollectionView

The ListCollectionView is a WPF wrapper for an IList. When you add or remove something from the IList, the ListCollectionView must be notified of the change by calling its OnCollectionChanged method so it can sync with the IList.

The problem is, OnCollectionChanged is a protected method i.e. we can't access it from outside of a ListViewCollection object. This means we have to create a new class that inherits from ListCollectionView so we can have the means to call OnCollectionChanged whenever we add or remove something from the IList object we are wrapping e.g.

class StudentListView(ListCollectionView):
	def constructor(theClass as Class): 
		super(theClass.StudentList) 
		theClass.StudentListChanged += OnStudentListChanged
	
	def OnStudentListChanged(sender as object, e as StudentListChangedEventArgs):
		if e.ChangeMode == Class.ChangeMode.Add:
			args = NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,e.Student,e.Index)
		else
			args = NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,e.Student,e.Index)
		OnCollectionChanged(args)

So whenever the parent Class object fires its StudentListChanged event after adding or removing a Student from its StudentList, then the StudentListView object gets to call the OnCollectionChanged method it inherited from ListCollectionView, allowing it to remain in sync with the underlying StudentList object.

Published 10-06-2006 9:57 PM by smash
Filed under: , ,