Why is the ObservableCollection not updated when items change?

I noticed that the ObservableCollection in WPF only reflects changes in the GUI by adding or removing an item in the list, but not by editing it.

This means that I have to write my own class MyObservableCollection. What is the reason for this behavior?

thanks

+6
wpf observablecollection
source share
5 answers

There is no way in ObservableCollection to find out if changes are made to the objects that it contains - if you want to be notified of changes to these objects, you also need to make these objects visible (for example, if these objects implement INotifyPropertyChanged)

+11
source share

Another way to achieve this would be to implement a new XXXViewModel class that comes from DependencyObject and you put it in an ObservableCollection.

for this look at this is a very good introduction to MVVM: http://blog.lab49.com/archives/2650

an example for such a class would be:

 public class EntryViewModel : DependencyObject { private Entry _entry; public EntryViewModel(Entry e) { _entry = e; SetProperties(e); } private void SetProperties(Entry value) { this.Id = value.Id; this.Title = value.Title; this.CreationTimestamp = value.CreationTimestamp; this.LastUpdateTimestamp = value.LastUpdateTimestamp; this.Flag = value.Flag; this.Body = value.Body; } public Entry Entry { get { SyncBackProperties(); return this._entry; } } public Int64 Id { get { return (Int64)GetValue(IdProperty); } set { SetValue(IdProperty, value); } } // Using a DependencyProperty as the backing store for Id. This enables animation, styling, binding, etc... public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(Int64), typeof(EntryViewModel), new UIPropertyMetadata(new Int64())); }} 

important things here: - it comes from DependencyObject - it works with DependencyProperties to support WPF data binding

w sargola

+1
source share

You can register the method in the view model class associated with the PropertyChanged event of the data class objects and listen to them in the View model when any change in the property of the data objects occurs. This is a very simple and easy way to have control over the View model when changing elements of the observed collection. Hope this helps ...

+1
source share

Probably because the elements cannot notify the collection when they are being edited, i.e. they may not be observed. Other classes would have similar behavior - in no way warn you about every change in the reference class schedule.

0
source share

As a job, you can retrieve an object from the collection and then reinsert it after processing is complete. Depending on your requirements and the concurrency model, this may just make the program ugly. This is a quick hack and is not suitable for anything that requires quality.

Instead, you can implement the collection using an update method that specifically fires the ContentChanged event (not sure of the name). It's ugly, but at least it's easy to handle.

Ideally, as kragen2uk says, it would be better to make objects visible and keep client code clean and simple.

see also this question .

0
source share

All Articles