Smooth scrolling for WPF DataGrid

别等时光非礼了梦想. 提交于 2019-12-30 02:42:17

问题


I am using WPF datagrid, only modification I have in place is:

    <toolkit:DataGridTextColumn.ElementStyle>
       <Style TargetType="TextBlock">
       <Setter Property="TextWrapping" Value="Wrap"/>
      </Style>
    </toolkit:DataGridTextColumn.ElementStyle>

I have this modification so if the cell contents are longer, they stretch the line height, no text is hidden. Problem is with DataGrid's scrolling behaviour - it jumps whole lines when scrolling, which does not work well at all if the row is higher than one line - scrollbar is jerking on scrolling etc.

Is there any way to make WPF DataGrid scroll "smoothly" and not line by line?

Thanks


回答1:


The DataGrid has an Attached property, ScrollViewer.CanContentScroll, that manages this behavior. To get smooth scrolling you'll need to set it to False.




回答2:


I haven't played with the DataGrid explicitly, but it is factual that using ScrollViewer.CanContentScroll=False swaps out the default ItemsPanelTemplate which uses the VirtualizedStackPanel with a regular StackPanel. It will scroll smoothly, but it will be rendering every item even if it's not visible.

This can absolutely kill performance if you're dealing with either a complex visual tree or large datasets.




回答3:


  <DataGrid Grid.Row="1"
              CanUserAddRows="False" 
              CanUserDeleteRows="False" 
              CanUserReorderColumns="False" 
              CanUserSortColumns="False" 
              SelectionUnit="FullRow" 
              HeadersVisibility="None"
              Name="grd" 
              GridLinesVisibility="None"
              ItemsSource="{Binding}"
              AutoGenerateColumns="False" 
              ScrollViewer.CanContentScroll="False">
</DataGrid>


来源:https://stackoverflow.com/questions/2303120/smooth-scrolling-for-wpf-datagrid

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