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
1415
Maintain scroll position after datasource refreshed
posted

How can I maintain the scoll position after the datasource has been refreshed?

I've tried: 

ScrollBehaviorOnListChange="PreserveRecordsInView"

and

ScrollBehaviorOnListChange="PreserveScrollOffset"

Without any luck.  I've also tried 

MyGrid.ScrollInfo.SetVerticalOffset(_scrollOffset); 

after the datasource changed, but the grid is still refreshed back to the top.

  • 34830
    Verified Answer
    Offline posted

    Hello PMac,

    I have been investigating into this issue, and I am curious as to how your data source is being "changed?" I am assuming that you are setting it to a new instance of your collection, as I have not been able to reproduce this issue by updating the already existing, bound collection. If my assumption is incorrect, please let me know, as the following corresponds to that assumption.

    If you are essentially "resetting" the data source, what this will do is it will clear and redraw the XamDataGrid's records. This will force the XamDataGrid back to the top, and if you wish to keep your records in view, I would recommend getting the index of the last record in view, and then bring that record into view after the update has happened. You may want to consider doing this inside of a Dispatcher.BeginInvoke action in order to ensure that the records have been drawn. This could be done by using the GetRecordsInView method of the XamDataGrid, and getting the index of the last one relative to the full Records collection. The relevant code for achieving this is below, where "xdg1" is the XamDataGrid:

    var x = xdg1.GetRecordsInView(true);
    DataRecord lastRecord = x[x.Count() - 1] as DataRecord;

    int index = xdg1.Records.IndexOf(lastRecord) - 1;

    xdg1.DataSource = null;
    xdg1.DataSource = vm.Data;

    Dispatcher.BeginInvoke(new Action(() =>
    {
          xdg1.BringRecordIntoView(xdg1.Records[index]);
    }));

    I have also attached a sample project demonstrating the usage of the ScrollBehaviorOnListChange property set to "PreserveRecordsInView" when updating the existing data source bound to the XamDataGrid. I hope this helps you.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer

    XDGScrollBehaviorCase.zip