Hi,
I have fairly straightforward code that refreshes the data in the grid. What I'd like to do is maintain the scrolling position.
Here's what I have:
double vOffsetBackup = myGrid.ScrollInfo.VerticalOffset;myGrid.DataSource = newData;myGrid.ScrollInfo.SetVerticalOffset(vOffsetBackup);
This, however, doesn't seem to work. The only way I could get it to work is by invoking SetVerticalOffset with a dispatcher, but then I get this flicker effect where the user can see the scroll bar moving.
Any suggestions on how to resolve this?
Hello Danny,
Thank you for sharing the approach that helped you solve the issue. As you have mentioned my approach was related to changing the items in the source collection rather than re-binding the whole collection. If you have any other questions on the matter, please feel free to post them here.
Strangely enough, I've been able to solve the problem by calling
myGrid.ScrollInfo.ScrollOwner.ScrollToVerticalOffset(vOffsetBackup);
I think what you're saying is that it's better to bind to a collection once, and then when I need to "rebind" just clear out the items in that collection and add items from a different one.
Hello Danny and thank you for posting!
In WPF replacing one collection with another makes the view, in this case the XamDataGrid control, to be bound to the old collection. A fix for this is firing the PorpertyChanged event to notify the view that the collection has been replaced. In that case the view unbinds from the old collection and binds to the new one. The view could not adjust to the items that were added or removed, and the whole control should rebind. This is not so efficient approach and will scroll back to the top of the list rather than preserving the user context. Reloading the items and scrolling to a specific item will make an expected flicker to the view which you have experienced with XamDataGrid. To overcome this issue you can consider using custom collection as data source. This collection can have properties for suspending the notifications and updating the collection. While the updates are suspended the data in the collection could be cleared and updated. Then the UpdateCollection method could be called to update the collection. This will preserve the scroll position and will remove the flickering behavior. Please note that the SuspendNotification property should be set again to false so that the other updates are processed in the collection.I hope this approach will be helpful for you. Please feel free to update the thread if you need additional assistance after reviewing the attached sample project.