I have a ListView
that is databound to an ObservableCollection
...
By default the ItemsSource
is of type IEnumerable. You need to first cast to a type that has access to the CollectionChanged event, then add a handler for that event.
((INotifyCollectionChanged)List1.ItemsSource).CollectionChanged +=
new NotifyCollectionChangedEventHandler(List1CollectionChanged);
public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
// Your logic here
}
ObservableCollection
, you don't need to. INotifyCollectionChanged
contains the event you need and if you ever decide to use some other type of collection that implements it, this will continue to work, whereas casting to an ObservableCollection
means that if you one day decide that you're list is now of type MyOwnTypeOfObservableCollectionNotDerivedFromObservableCollection
than this will break. ;)
P.S. This should go in the xaml code-behind.