1

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 Answers1

1

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.)

  • Most likely the best answer from a new contributor I have ever seen. Well done, congrats. – Rand Random Aug 18 '22 at 19:50
  • Hi Thanks for the fast reply! Unfortunately it didn't work. When the property of the is changed, the collection didn't fire the PropertyChanged event. –  Aug 18 '22 at 20:05
  • @Wiley - That’s not what the propertychanged of the observablecollection does. It only fires the event when a property of the collection itself change, and NOT for items within the collection. You would need to register on each items propertychanged individually. – Rand Random Aug 18 '22 at 20:11
  • @Wiley - see here: https://stackoverflow.com/questions/15698195/observablecollection-collectionchanged-not-firing or here: https://stackoverflow.com/questions/37068379/observablecollection-onpropertychanged-not-firing – Rand Random Aug 18 '22 at 20:12
  • Got it! eventually I created a setter event to detect all property changes! thanks! –  Aug 19 '22 at 11:41