0

I have a simple ViewModel containing an ObservableCollection which is bound to the ItemsSource prop. of a ListView. On predefined time period the ViewModel will receive an update (snapshot) - new DataObject that has the same Id but different value. What i am doing right now is:

 - find the index of the item with this id
 - if there is no such item: add it to the collection
 - if there is - replace it:   obsColItems[index] = newDataObject;

I expected the ListView to reflect the changes but only add operations are visible - subsequent changes (replacing of item) are not visible. I hooked up a CollectionChange event and it is correctly fired but ListView still shows the initial data.

Few thing I tried:

1. remove and then insert the item at the appropriate index - that
    leaved me with a bad taste in the mouth (it also messed the current
    selected item).
 2. After bit of research `BindingList` was mentioned in few similar SO questions
    and it did the trick - the `ListView` is now updating, but it seams to
    be full of functionality I don't relay need.

Is there something wrong with ObservableCollection and item replacing and how to make ListView to update after item replace ?

Tyronne
  • 135
  • 7
  • 1
    it seams that Equals method plays a role here - if DataObject overrides Equals to check only the ID (as it should) the list is not updated (although the collection change event is correctly fired with Replace state). if Equals is not overridden (I linearly search the index of the item to match the ID) then the ListView is updated as expected, I assume that ListView is making a check to see if there is really a need to repaint the cell of the item. In this case it should but the Equals method returns true as the snapshot has the same id and the cell is not repainted. – Tyronne Aug 04 '15 at 15:41

1 Answers1

0

I waited a long time someone to answer (I hate it when someone answers his own questions but ...).

The observable collection maintains snapshots of DataObject items and as DataObject Equals method is overridden to compare two objects only by ID the items change binding will not be triggered no mater that the data inside the object has changed.
The solution is to wrap the DataObject into another object that provides prop. change notification. Then all works as expected - replacing the snapshot inside the wrapper will trigger item change binding

Tyronne
  • 135
  • 7