Xceed DataGrid Resets ScrollBar Position

六眼飞鱼酱① 提交于 2019-12-11 01:04:52

问题


When I change the ItemsSource in the Xceed DataGridControl my vertical and horizontal scroll bars immediately get reset to the top/left.

Any ideas how to prevent that from happening?


回答1:


I finally fixed and figured out why my scrollbars jump to the top/left each time my DataGrid refreshes.

Turns out the XAML binded to the View instead of to an actual datasource (DataView), thus each refresh replaced the view and the datasource. As a result of binding to a DataView, my scrollbars no longer jump, and my grid now populates instantly as before it tool 1-2 seconds.

I included my code changes in case that helps others in the future.

Old code binds to view:

 <xcdg:DataGridControl Name="FileGrid"
                       AutoCreateColumns="False"
                       SelectionMode="Extended" 
                       ReadOnly="True"         
                       ItemsSource="{Binding FileGridDataSource}"
                       ItemScrollingBehavior="Immediate" 
                       NavigationBehavior="RowOnly">
 </xcdg:DataGridControl>

 public sealed class DataGridViewModel : ViewModelBase
 {
   public DataGridCollectionView FileGridDataSource
   {
      get
      {
         return _fileGridDataBoundSource;
      }
      set
      {
         _fileGridDataBoundSource = value;
         NotifyPropertyChanged("FileGridDataSource");
      }
   }
 }

New code binds to a DataView:

<Window.Resources>
  <xcdg:DataGridCollectionViewSource x:Name="FileGridView"
      x:Key="fileView"
      Source="{Binding Path=GridData}"
      AutoFilterMode="And"
      AutoCreateItemProperties="True"
      AutoCreateForeignKeyDescriptions="True"
      DefaultCalculateDistinctValues="False"/>
</Window.Resources>

<Grid>
  <xcdg:DataGridControl Name="FileGrid"
                        AutoCreateColumns="False"
                        SelectionMode="Extended" 
                        ReadOnly="True"         
                        ItemsSource="{Binding Source={StaticResource fileView}}" 
                        ItemScrollingBehavior="Immediate"  
                   NavigationBehavior="RowOnly">
  </xcdg:DataGridControl>
</Grid>

public sealed class DataGridViewModel : ViewModelBase
{
   private DataTable _dt = new DataTable("MyDataTable");
   public DataView GridData
   {
      get
      {
         return _dt.DefaultView;
      }
   }
}


来源:https://stackoverflow.com/questions/2792158/xceed-datagrid-resets-scrollbar-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!