Why doesn't my ObservationCollection have any PropertyChanged event?

Viewed 43

enter image description here

Why doesn't my ObservationCollection have any PropertyChanged event? I checked the documentation and it should contain a PropertyChanged event that's triggered every time the property of the class gets changed. Is it not the case?

public class ControlObjectsPropertyOnHold  //TODO change to ObservableCollection
{
    public string Name { get; set; }
    public Rhino.DocObjects.ObjRef ObjRef { get; set; }
    public System.Guid ObjGuid { get; set; }

    public ControlObjectsPropertyOnHold(string name, Rhino.DocObjects.ObjRef objRef, System.Guid objGuid)
    {
        if (null != name)
        {
            Name = name;
            ObjRef = objRef;   //Place-holders in case there are more to store
            ObjGuid = objGuid;
        }
    }
        }

Edit: Took EventHorizon's advice but still doesn't work. When ControlObjectsPropertyOnHold.Name is changed, nothing fires.

enter image description here.

1 Answers

It has the PropertyChanged event. It actually has two.

The first is the public PropertyChanged event as specified by the INotifyPropertyChanged interface. But ObservableCollection implements it as an explicit interface implementation (note how the respective _ObservableCollection<T>_documentation shows that the event is implemented as System.ComponentModel.INotifyPropertyChanged.PropertyChanged -- the interface name as part of the event name indicates an explicit interface implementation; the Remarks section of this documentation also mentions this fact). This event only becomes accessible when you cast the ObservableCollection instance as INotifyPropertyChanged.

var myObsColl = new ObservableCollection<string>();
((INotifyPropertyChanged) myObsColl).PropertyChanged += (sender, ea) => {};

The second is a protected PropertyChange event. But this is not an implementation of the event from the INotifyPropertyChanged interface, because of its protected access modifier. Internally, ObservableCollection<T> forwards (un)subscriptions to the public INotifyPropertyChanged.PropertyChanged event to this protected event (see the related implementation in the dotnet/runtime github repo).

(Side note: Typically, you would not need to care about ObservableCollection<T>'s PropertyChanged event. It should be sufficient to subscribe to the CollectionChanged event -- whenever this event is raised you know the content of the ObservableCollection<T> instance has changed.)

Related