Mouse scroll not working in a scroll viewer with a wpf datagrid and additional UI elements Mouse scroll not working in a scroll viewer with a wpf datagrid and additional UI elements wpf wpf

Mouse scroll not working in a scroll viewer with a wpf datagrid and additional UI elements


I think Dave's solution is a good one. However, one recommendation I'd make is to catch the PreviewMouseWheel event on the scrollviewer instead of on the datagrid. If you don't, you might notice some minor differences based on whether you're scrolling over the datagrid or the scroll bar itself. The reasoning is that the scrollviewer will be handling scrolling when the mouse is hovered over the scrollbar, and the datagrid event will handle the scrolling when over the datagrid. For instance, one mouse wheel scroll over the datagrid might bring you farther down your list then it would when over the scroll bar. If you catch it on scrollviewer preview event, all will use the same measurement when scrolling. Also, if you catch it this way, you won't need to name the scrollviewer element, as you won't need a reference to the object since you can just type cast the sender object passed into the scrollviewer PreviewMouseWheel event. Lastly, I'd recommend marking the event handled at the end of the event, unless you need to catch it in an element further down the heirarchy for some reason. Example below:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)    {        ScrollViewer scv = (ScrollViewer)sender;        scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);        e.Handled = true;    }


I'm assuming the DataGrid does not need to scroll - Set the VerticalScrollBar="None" on the DataGrid.

The DataGrid swallows the mouse scroll event.

What I found is to use the PreviewMouseWheel event to scroll the container that you want to scroll. You will need to name the scrollviewer to change the Vertical offset.

private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)    {       scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset-e.Delta);    }


An improvement to Don B's solution is to avoid using ScrollToVerticalOffset.

scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);

VerticalOffset - Delta results in a pretty jarring experience. The ScrollViewer puts a lot of thought into making the movement smoother than that. I think it also scales the delta down based on dpi and other factors...

I found it's better to catch and handle the PreviewMouseWheelEvent and send a MouseWheelEvent to the intended ScrollViewer. My version of Don B's solution looks like this.

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e){    var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);    eventArg.RoutedEvent = UIElement.MouseWheelEvent;    eventArg.Source = e.Source;    ScrollViewer scv = (ScrollViewer)sender;    scv.RaiseEvent(eventArg);    e.Handled = true;}