Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The mouse wheel event doesn't work correcty on a lisbox when it has scrollviewers inside

I have an outer listbox with a vertical scrollbar, and on each item I have a scrollviewer that might have a horizontal scrollbar. The problem is that when I use the mouse the event doesn't get to the outer listbox, so scrolling doesn't work. I have already set Focusable=false on the scrollviewers, but that just prevents them for handling keyboard events, not mouse events. How can I stop the inner scrollviewer from catching the mouse wheel event and allow it to bubble up to the outer listbox?

like image 264
Gustavo Guerra Avatar asked Nov 04 '22 07:11

Gustavo Guerra


2 Answers

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

like image 99
Rachel Avatar answered Dec 08 '22 11:12

Rachel


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

You need to handle the preview mouse wheel events on the ListBox, and thus prevent them from tunnelling further down, while at the same time, raise a 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);
}
like image 25
AlexPi Avatar answered Dec 08 '22 12:12

AlexPi