views:

143

answers:

1

Hi,

I have encountered a problem when I refresh the observable collection behind my Silverlight DataGrid (the DataGrid binds to a PagedCollectionView with an ObservableCollection as source).

When I clear the observable collection and add the new items to the collection the DataGrid repositions to the top. I want the DataGrid to keep the scroll position. This seems to somewhat happen if I remove the items in the collection one-by-one with the RemoveAt mehtod and the add the new items one-by-one. But why is there this difference between removing all the items at once with the Clear method and removing the items one-by-one with the RemoveAt method?

Best Regards, Jesper

A: 

The DataGrid keeps track of scroll position as the top most item. When you Clear all items the DataGrid scroll no longer exists because there are no items. In order for scroll position to make any logical sense the DataGrid must contain at least one item.

The problem is likely coming from the fact that using an ObservableCollection when you call clear it throws the CollectionChanged event with EventArgs.Action set to NotifyCollectionChangedAction.Reset not to remove. If you are set on using Clear than you will probably need to implement your own INotifyCollectionChanged collection that's doesn't ever throw NotifyCollectionChangedAction.Reset eset.

Stephan
But when I remove the items one-by-one, I also end up with no items in the DataGrid before I start added the new items.
Krimson