Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a scroll event to DataGrid

Tags:

c#

wpf

datagrid

I have a DataGrid defined as follows as part of a UserControl:

<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'd like to add an event on when the user drags horizontally on the DataGrid, it updates another chart I have. Can someone point me in the direction to get this started? Thanks.

like image 995
Crystal Avatar asked Sep 06 '11 21:09

Crystal


2 Answers

If I understand your question correctly you want to find out when the user has scrolled the DataGrid Horizontally. This can be done with the attached event ScrollViewer.ScrollChanged.

Xaml

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

Code behind

private void dtGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (e.HorizontalChange != 0)
    {
        // Do stuff..
    }
}
like image 188
Fredrik Hedblad Avatar answered Nov 19 '22 11:11

Fredrik Hedblad


If by 'drags horizontally' you mean 'scrolls horizontally' then you can use the ScrollViewer.ScrollChanged event. The ScrollChangedEventArgs contain properties such as HorizontalOffset and HorizontalChange.

like image 34
Philipp Schmid Avatar answered Nov 19 '22 10:11

Philipp Schmid