ObservableCollection - notification when item has changed (not the collection)

D

David Ching

Hello,

I have defined:

public class RawStat : INotifyPropertyChanged
{
...
}


public class RawStatCollection : ObservableCollection<RawStat>
{
...
}


void rawStats_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
MessageBox.Show("RawStats changed");
}

var rawStats = new RawStatCollection();
rawStats.CollectionChanged += rawStats_CollectionChanged;



rawStats_CollectionChanged() is properly called when I add something to the
collection , e.g.

rawStats.Add (new RawStat);


But if the RawStat itself is changed, even though it implements
INotifyPropertyChanged, I am not getting a notification for
rawStats_CollectionChanged. I would argue that I should be, since changing
an item in a collection is changing the contents of the collection itself.

But, putting aside theoretical debate, is there any easy way to get notified
when the RawStat has changed, short of sinking the PropertyChanged event for
every RawStat I add to the collection?

Thanks,
David
 
H

Henning Krause [MVP - Exchange]

Hello David,

you'll have to bind to the PropertyChanged event of each row.

You could derive a collection from ObservableCollection to automate this.

Kind regards,
Henning Krause
 
P

Pavel Minaev

Hello,

I have defined:

public class RawStat : INotifyPropertyChanged
{
...
}

public class RawStatCollection : ObservableCollection<RawStat>
{
...
}

void rawStats_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
MessageBox.Show("RawStats changed");
}

var rawStats = new RawStatCollection();
rawStats.CollectionChanged += rawStats_CollectionChanged;

rawStats_CollectionChanged() is properly called when I add something to the
collection , e.g.

rawStats.Add (new RawStat);

But if the RawStat itself is changed, even though it implements
INotifyPropertyChanged, I am not getting a notification for
rawStats_CollectionChanged. I would argue that I should be, since changing
an item in a collection is changing the contents of the collection itself.

But, putting aside theoretical debate, is there any easy way to get notified
when the RawStat has changed, short of sinking the PropertyChanged event for
every RawStat I add to the collection?

For an ObservableCollection<T>, no - you have to extend it and write
your own class that would subscribe to all items, and propagate their
PropertyChanged events outside.
If you are not restricted to using ObservableCollection<T> for
whatever reason, then you might want to consider BindingList<T> - it
does propagate PropertyChanged events.
 
D

David Ching

Pavel Minaev said:
For an ObservableCollection<T>, no - you have to extend it and write
your own class that would subscribe to all items, and propagate their
PropertyChanged events outside.
If you are not restricted to using ObservableCollection<T> for
whatever reason, then you might want to consider BindingList<T> - it
does propagate PropertyChanged events.

Thank you Pavel, and Henning too. I am writing a WPF app that makes
extensive use of INotifyPropertyChanged. The reason for using
ObservableCollection is that it implements this interface. AFAIK, this
interface is required to be used with WPF UI elements that have a
"{Binding}" in them, so that the screen is updated when the object changes.
BindingList does not seem to implement INotifyPropertyChanged; it implements
IBindingList, which I'd not heard of before.

Do you know if this can be used in WPF which expects INotifyPropertyChanged?

Thanks,
David
 
P

Pavel Minaev

Thank you Pavel, and Henning too.  I am writing a WPF app that makes
extensive use of INotifyPropertyChanged.  The reason for using
ObservableCollection is that it implements this interface.  AFAIK, this
interface is required to be used with WPF UI elements that have a
"{Binding}" in them, so that the screen is updated when the object changes..
BindingList does not seem to implement INotifyPropertyChanged; it implements
IBindingList, which I'd not heard of before.

Do you know if this can be used in WPF which expects INotifyPropertyChanged?

IBindingList is the "old" non-generic interface for observable
collections dating back to WinForms. It does not implement
INotifyCollectionChanged (which is actually what you need to properly
bind to a collection in WPF), but, as far as I know, WPF does
specifically support IBindingList in addition to
INotifyCollectionChanged (so that you can use WPF with your old,
WinForms-era business objects). Your best bet is to prototype it and
see how it goes.
 
D

David Ching

Pavel Minaev said:
IBindingList is the "old" non-generic interface for observable
collections dating back to WinForms. It does not implement
INotifyCollectionChanged (which is actually what you need to properly
bind to a collection in WPF), but, as far as I know, WPF does
specifically support IBindingList in addition to
INotifyCollectionChanged (so that you can use WPF with your old,
WinForms-era business objects). Your best bet is to prototype it and
see how it goes.

Will do, thanks much Pavel!

-- David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top