Crystal
Crystal

Reputation: 29458

Adding a scroll event to 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.

Upvotes: 7

Views: 12619

Answers (2)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

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..
    }
}

Upvotes: 19

Philipp Schmid
Philipp Schmid

Reputation: 5828

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.

Upvotes: 3

Related Questions