Bubbling scroll events from a ListView to its parent

Viewed 23185

In my WPF application I have a ListView whose ScrollViewer.VerticalScrollBarVisibility is set to Disabled. It is contained within a ScrollViewer. When I attempt to use the mouse wheel over the ListView, the outer ScrollViewer does not scroll because the ListView is capturing the scroll events.

How can I force the ListView to allow the scroll events to bubble up to the ScrollViewer?

8 Answers

Thanks Keyle

I adapted your answer as an RX extension method

    public static IDisposable ScrollsParent(this ItemsControl itemsControl)
    {
        return Observable.FromEventPattern<MouseWheelEventHandler, MouseWheelEventArgs>(
           x => itemsControl.PreviewMouseWheel += x,
           x => itemsControl.PreviewMouseWheel -= x)
           .Subscribe(e =>
           {
               if(!e.EventArgs.Handled)
               {
                   e.EventArgs.Handled = true;
                   var eventArg = new MouseWheelEventArgs(e.EventArgs.MouseDevice, e.EventArgs.Timestamp, e.EventArgs.Delta)
                   {
                       RoutedEvent = UIElement.MouseWheelEvent,
                       Source = e.Sender
                   };
                   var parent = ((Control)e.Sender).Parent as UIElement;
                   parent.RaiseEvent(eventArg);
               }
           });
    }

Usage:

 myList.ScrollsParent().DisposeWith(disposables);
Related