Mouse wheel event does not work properly on lisbox when it has scrollviewers inside

I have an external list with a vertical scrollbar, and on each item I have a scrollviewer that can have a horizontal scrollbar. The problem is that when I use the mouse, the event does not fall into the external list, so scrolling does not work. I already set Focusable = false in scrollviewers, but that just prevents them from handling keyboard events, not mouse events. How can I stop the internal scrollviewer from catching the mouse wheel event and letting it go to the external list?

+4
source share
2 answers

You can find some good examples here. It describes how to disable the mouse wheel in ItemsControl

+1
source

The problem is that the ListBox itself has a ScrollViewer that absorbs mouse wheel events before they can get to the parent ScrollViewer that contains your ListBox.

You need to handle the mouse preview events in the ListBox and thus prevent them from tunneling down, at the same time, raise the bubbling event to the parent ScrollViewer.

This worked for me:

 private void ListBoxThatNowScrolls_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = true; var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta); e2.RoutedEvent = ListBox.MouseWheelEvent; e2.Source = e.Source; ListBoxThatNowScrolls.RaiseEvent(e2); } 
0
source

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


All Articles