2

I have noticed a situation where if I remove all the items from the ObservableCollection using RemoveRange, it does update the UI but if there is at least single item left, it will not. By looking at ObservableCollection source code here, I could not see specific that they were doing to refresh the internal Items collection. I am basically sub classing the ObservableCollection and implementing my own OnCollectionChanged behavior to prevent UI peformance issue and facing this problem. Any idea in this direction? Any pointer on how to refresh observablecollection?

Thanks, Jay

Jay Nanavaty
  • 1,089
  • 1
  • 15
  • 29

2 Answers2

1

Do you use OldItems and NewItems in NotifyCollectionChangedEventArgs when your UI activate in OnCollectionChanged?

ClearItems method or Add/Remove Range method in @CarbineCoder answer's link don't make OldItems and NewItems to NotifyCollectionChangedEventArgs. it only have NotifyCollectionChangedAction.Reset.

I think if your UI code modified according to OldItems and NewItems it does not working on call ClearItems or custom implemented Range methods like in link.

pius lee
  • 1,164
  • 1
  • 12
  • 28
0

In short Observable collection triggers NotifyProperty changed event for changes made to the underlying collection. So if you see the public methods available in the Observable collection Source code you mentioned you will see implementation of InsertItem, RemoveItem, ClearItems etc.. they call OnPropertyChanged(IndexerName);

As far as RemoveRange method it is not implemented by ObservableCollection but by the underlying List which does not call the OnPropertyChanged(IndexerName); hence you dont observe the changes in the UI. I suggest you not to use remove range but loop through items and remove them one by one.

You can see the implementation for removerange - https://stackoverflow.com/a/670579/442444

Community
  • 1
  • 1
Carbine
  • 7,849
  • 4
  • 30
  • 54
  • There is a performance hit when I remove them one by one or add them one by one. It causes say 3000+ UI notifications for the bound DataGrid control and freezes the UI. – Jay Nanavaty Sep 07 '16 at 10:02
  • See the link provided above, it has the implementation to notify only once for a range. – Carbine Sep 07 '16 at 10:04
  • Thanks. I know about that link already. I tried it actually and following the same approach but still I do not see changes into the UI. I also explicitly call property changed for Items[] and Count as well. – Jay Nanavaty Sep 07 '16 at 10:07