1

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

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • What do you mean with "I want this to work on a collection"? Add, Remove item on collection? – ADIMO Oct 31 '11 at 13:40
  • Can you include the code paths where you are changing the IsPlanned property? – Johannes Kommer Oct 31 '11 at 14:11
  • Note: the first line I just added in that edit is the one that causes the message box to display 5 times, the 2nd line works fine (just displays msg once). –  Oct 31 '11 at 14:15
  • Looking at the code you posted, it should only be called once. I can't seem to reproduce 5 calls in my test-code either. Have you tried attaching a debugger to see how the code is called, and if there are any loops causing issues? – Johannes Kommer Oct 31 '11 at 14:26
  • I'm starting this shit again. Code's got out of hand, haven't got a clue why it's doing it 5 times and more and more is goin wrong with it. Thanks anyway. –  Oct 31 '11 at 14:48

2 Answers2

2

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.

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • Of course yer, thanks again J.Kommer, been a big help today mate. –  Oct 31 '11 at 13:38
  • Ahh wait, your edit just confused me haha I'm gonna have to read into how to do this. It isn't really important that I achieve this on the collection come to think of it. I need to monitor any changes made to any properties in the job class (all jobs will be found in this collection anyway), and the event handler will call a method in a totally different singleton class. I'll work out how to implement it anyway, I think this may be the link: http://msdn.microsoft.com/en-us/library/ms229614.aspx –  Oct 31 '11 at 13:45
  • That link above makes it seem so simple - I'm not quite sure why you would need to inherit the INotifyPropertyChanged class but I'm pretty sure that's my answer. –  Oct 31 '11 at 13:46
  • Sorry about the confusion, it'll still work separately of your collection just as well. My edit was referring to the fact that you could also do it over the entire collection, once the interface has been implemented. – Johannes Kommer Oct 31 '11 at 13:48
  • The main reason to inherit from the interface is to allow for generic code, i.e. you can write methods which will work on anything implementing the interface + anything from the .NET framework that works on those interfaces works instantly for your code as well. Imagine if in the future you also have a `User` class for which you want to do similar behaviour, when a property changes, as for `Job`. – Johannes Kommer Oct 31 '11 at 13:52
  • Yer you're spot on, and I'll keep it in mind for if I need this to go that way. Thanks again –  Oct 31 '11 at 13:53
  • J.Kommer - see edit please, bit more of an explanation, I'm stuck on the last hurdle now really, if I can get this line of code to run just the once it does exactly what I need it to. –  Oct 31 '11 at 14:06
0

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.

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116