ObservableCollection PropertyChanged event

c#, mvvm, observablecollection, propertychanged, wpf

Solution

SelectableList<int> intList = new SelectableList<int>();
((INotifyPropertyChanged)intList).PropertyChanged += 
    new PropertyChangedEventHandler(intList_Changed);

ObservableCollection implements INotifyPropertyChanged explicitly, which means you have to cast the instance to the interface before you can access the interface's methods, properties and events. As to why this is done, I don't know. The Binding markup extension doesn't "know" ObservableCollections or any other type. It checks types to see if they implement or extend specific interfaces/base classes (INPC, INCC, DependencyObject, etc) and so doesn't care if the interface is implemented explicitly.

Problem

I want to subclass `ObservableCollection` to add a property to it. Unfortunately, the `PropertyChanged` event is protected. Basically, I want to subclass it to have a `SelectedItem` that I can bind to for lists in my MVVM WPF app. Here's the skeleton of my class: ``` public class SelectableList<T> : ObservableCollection<T> { public T SelectedItem {get;set;} } ``` But I cannot do the following: ``` SelectableList<int> intList = new SelectableList<int>(); intList.PropertyChanged += new PropertyChangedEventHandler(intList_Changed); ``` because of access restrictions. This causes me to ask a deeper question. How is the UI notified of `PropertyChanged` events (e.g. Count property)? Note that I cannot do it in a code-behind. My head is spinning, can someone please enlighten me?

Original source