5

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Gustavo Guerra
  • 5,319
  • 24
  • 42
  • 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. – hyp Jan 26 '12 at 14:18
  • You might find some good answers on this question: http://stackoverflow.com/q/2189053/302677 – Rachel Jan 26 '12 at 15:20
  • Thanks for the link Rachel, I defined a NoWheelScrollViewer as described there and it worked – Gustavo Guerra Jan 27 '12 at 22:10
  • @Rachel, it would be helpful if you could copy your comment into an answer and if ovatsus could mark this question as answered. – Sheridan Feb 11 '12 at 00:23

2 Answers2

1

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);
}
AlexPi
  • 539
  • 5
  • 16
1

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

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490