Adding a scroll event to a DataGrid

I have a DataGrid as indicated in the UserControl part:

<DataGrid x:Name="dtGrid" AutoGenerateColumns="False" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode ="Standard" EnableColumnVirtualization="True" EnableRowVirtualization="True" ScrollViewer.IsDeferredScrollingEnabled="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="True" ItemsSource ="{Binding}" Block.TextAlignment="Center" AlternatingRowBackground="#F1F1F1" RowBackground="White" CanUserAddRows="False" CanUserDeleteRows="False" FrozenColumnCount="1" GridLinesVisibility="None" > </DataGrid> 

I would like to add an event when the user drags horizontally to the DataGrid, it updates the other chart that I have. Can someone point me in the direction to get this started? Thanks.

+6
source share
2 answers

If I understand your question correctly, you want to know when the user scrolled the DataGrid Horizontally. This can be done when the ScrollViewer.ScrollChanged event is ScrollViewer.ScrollChanged .

Xaml

 <DataGrid x:Name="dtGrid" ScrollViewer.ScrollChanged="dtGrid_ScrollChanged" ... /> 

Code for

 private void dtGrid_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (e.HorizontalChange != 0) { // Do stuff.. } } 
+16
source

If "drag horizontally" means "scrolls horizontally", you can use the ScrollViewer.ScrollChanged event. ScrollChangedEventArgs contain properties such as HorizontalOffset and HorizontalChange .

+3
source

Source: https://habr.com/ru/post/896748/


All Articles