开发者

Silverlight DataGrid refresh observable collection

开发者 https://www.devze.com 2022-12-30 08:12 出处:网络
I have encountered a problem when I refresh the observable collection behind my Silv开发者_如何学Cerlight DataGrid (the DataGrid binds to a PagedCollectionView with an ObservableCollection as source).

I have encountered a problem when I refresh the observable collection behind my Silv开发者_如何学Cerlight 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 method 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消