ObservableCollections and changes to properties in C#

564 views Asked by At

I'm working with an observable collection of a Job class I have defined. I have binded a method to handle the INotifyCollectionChanged event. MSDN tells me that INotifyCollectionChanged is a "listener of dynamic changes, such as when items get added and removed or the whole list is refreshed," but I'd like to listen for changes to properties to any of the job classes in the collection, is there an event handler for this?? I understand there is an INotifyPropertyChanged interface but I want this to work on a collection.

EDIT:

I'm confused by this to be honest so I should give more background info to what I'm doing so I can get my answer. I have this property in a 'Job' class:

    public Boolean IsPlanned
    {
        get
        {
            return this.Storage<Job>().isPlanned;
        }
        set
        {
            var storage = this.Storage<Job>();

            if (storage.isPlanned != value)
            {
                storage.isPlanned = value;
                this.OnPropertyChanged(() => this.isPlanned);
                MessageBox.Show("IsPlanned property was changed on one of the jobs " + this.Subject);
            }
        }
    }

This job class actually inherits from a telerik control's appointment class (which just so happens to implement INotifyPropertyChanged). From telerik documentation I also got the above code (minus the messagebox). Now when I'm changing this boolean ONCE, that message box line is bein executed 5 times.

Any help appreciated!!

EDIT 2: Paths were IsPlanned is changed:

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)state.DraggedAppointments.First()).JobGuid)).IsPlanned = true;

PresentationManager.Instance.AllJobs.Single(o => o.JobGuid.Equals(((Job)payload.DraggedAppointments.First()).JobGuid)).IsPlanned = false;

These are both from different classes that are used to define override's for my custom drag drop behaviour (from listbox).

2

There are 2 answers

7
Johannes Kommer On BEST ANSWER

Implement the INotifyPropertyChanged interface on your Job class. This should then allow you to use the PropertyChanged on your ObservableCollection<Job>.

To fully support transferring data values from binding source objects to binding targets, each object in your collection that supports bindable properties must implement an appropriate property changed notification mechanism such as the INotifyPropertyChanged interface.

0
wageoghe On

Here is one example from here on StackOverflow of implementing an ObservableCollection that also raises events when contained elements are modified:

ObservableCollection that also monitors changes on the elements in collection

See Reed Copsey's answer in this thread for a link to a project that has implmented an ObservableCollection that listens to its child elements.