Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
195
Maintain scroll position after rebinding
posted

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?

Parents
No Data
Reply
  • 17475
    Offline posted

    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.

    ResetScroll.zip
Children