-1

Current realization

Hello there. Currently I have the following application:

enter image description here

If you can see, when my cursor on blue or red zone I`am scrolling only the main ScrollViewer.

Expectations

I'am expecting that if I'll scroll with cursor on DataGrid, should be scrolled only the DataGrid. Not the main ScrollViewer. But currently it works like the following demo:

enter image description here

Question

How can I enable the possibility to scroll DataGrid at ScrollViewer?

Current code

<ScrollViewer x:Name="ScrollViewer" PanningMode="Both" Background="Blue"
                HorizontalScrollBarVisibility="Auto" Padding="0,0,50,0"
                VerticalScrollBarVisibility="Auto" PreviewMouseWheel="DataGrid_PreviewMouseWheel">

    <StackPanel>
        <DataGrid x:Name="DataGrid0" ItemsSource="{Binding Items}" Height="300"
                    AutoGenerateColumns="True" PreviewMouseWheel="DataGrid_PreviewMouseWheel"
                    HorizontalScrollBarVisibility="Auto"
                    VerticalScrollBarVisibility="Auto"/>
        <Rectangle Height="200" Fill="Red"/>
        <DataGrid x:Name="DataGrid1" ItemsSource="{Binding Items}" Height="300"
                    AutoGenerateColumns="True" PreviewMouseWheel="DataGrid_PreviewMouseWheel"
                    HorizontalScrollBarVisibility="Auto"
                    VerticalScrollBarVisibility="Auto"/>
    </StackPanel>

</ScrollViewer>
private void DataGrid_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
    var args = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
    args.RoutedEvent = ScrollViewer.MouseWheelEvent;

    if (e.Source is DataGrid)
    {
        DataGrid0.RaiseEvent(args);
    }
    else
    {
        ScrollViewer.RaiseEvent(args);
    }
}
limeniye
  • 31
  • 1
  • 6
  • Try setting [MouseWheelEventArgs.Handled](https://learn.microsoft.com/en-us/dotnet/api/system.windows.routedeventargs.handled?view=windowsdesktop-7.0) to `true` in your `DataGrid_PreviewMouseWheel` event handler to stop other handlers from scrolling – Legkov Ivan Aug 17 '23 at 08:54
  • @LegkovIvan, thank you for your answer. But I already tried it. Not working. – limeniye Aug 17 '23 at 12:47

1 Answers1

1

Remove PreviewMouseWheel event from Scrollviewer and DataGrid too.