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

2

6

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?

Lerma answered 26/1, 2012 at 14:9 Comment(4)
Are you sure you're not handling the event somewhere and setting Handled = true ? MouseWheelEvent is a RoutedEvent so it should route/bubble nicely. Probably you'll need to show some XAML.Petronel
You might find some good answers on this question: https://mcmap.net/q/466098/-disable-mouse-wheel-on-itemscontrol-in-wpf/302677Tusche
Thanks for the link Rachel, I defined a NoWheelScrollViewer as described there and it workedLerma
@Rachel, it would be helpful if you could copy your comment into an answer and if ovatsus could mark this question as answered.Sprocket
T
1

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

Tusche answered 11/2, 2012 at 3:19 Comment(0)
P
4

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);
}
Pylos answered 26/10, 2017 at 8:27 Comment(1)
Thank you so much! I had a feeling something like this was happening. For my situation, this was the best solution.Brusquerie
T
1

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

Tusche answered 11/2, 2012 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.