Search code examples
wpfdatagridscrollviewer

WPF datagrid BringIntoView behaviour


In a WPF datagrid, when the user clicks a partially visible row at the bottom of the grid, the row will be brought into view. The grid will scroll enough to get the clicked row completely visible, such that the top row is still completely visible, meaning that there will be another partially visible row at the bottom where the user clicked.

With this behaviour, if the user double clicks the partially visible row at the bottom, the grid will move up two rows and the row beneath the initially selected row will be selected.

This behaviour can be changed by setting ScrollViewer.CanContentScroll="False" on the datagrid. with this setting, the partially visible row will be brought into view and will be the bottom most visible row, while the top most row will become partially visible.

However, this disables row virtualisation on the grid, which I do not want. Is there any other way to change the BringIntoView function without disabling row virtualisation?


Solution

  • Changing the attached property VirtualizingPanel.ScrollUnit to Pixel on your DataGrid should provide the behavior you need.

    <DataGrid ItemsSource="{Binding Data}" VirtualizingPanel.ScrollUnit="Pixel">
        ...
    </DataGrid>