how to keep vertical scroll bar on a previous position after refreshing data grid using MVVM design pattern?
this is our approach which also keeps the horizontal scroll position
call this method before you refresh:gridVerticalScrollPosition and gridHorizontalScrollPosition are private variables
private void getGridScrollPosition() { if (grdData.ScrollInfo != null) { gridVerticalScrollPosition = grdData.ScrollInfo.VerticalOffset; gridHorizontalScrollPosition = grdData.ScrollInfo.HorizontalOffset; } }
then after refresh, scroll the grid back doing something like this:
private void scrollGridToPreviousPosition() { if (grdData.ScrollInfo != null) { Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, (System.Threading.ThreadStart)delegate { grdData.ScrollInfo.SetVerticalOffset(gridVerticalScrollPosition); }); Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, (System.Threading.ThreadStart)delegate { grdData.ScrollInfo.SetHorizontalOffset(gridHorizontalScrollPosition); }); } }
Hello,
Thank you for contacting Infragistics. Are you updating records or completely resetting the datasource?
1. If you are resetting the datasource I recommend using the XamDataGrid BringRecordIntoView method and pass the record of the last record in view.
eg.
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]);}));
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]);}));
https://ko.infragistics.com/community/forums/t/107853.aspx
2. If you are replacing the collection with another then you may want to suspend PropertyChanged notifications from firing to notify the view that the collection was changed.
https://ko.infragistics.com/community/forums/t/94712.aspx
Let me know if you have any questions regarding this matter.
Sincerely,
Michael Di FilippoAssociate Software DeveloperInfragistics, Inc.www.infragistics.com/support