made attached dp 'ScrollViewerCorrector.FixNestedScrolling' to prevent nested ScrollViewers from eating MouseWheel events (effectively blocking parents from scrolling)

This commit is contained in:
John Colvin committed 2019-03-28 13:45:11 -05:00
1 parent eef792d148
commit 6ee4aea794
3 files changed
+60

No files matched your search

@@ -1,8 +1,15 @@
using System;
using System.Windows;
namespace mbts.ui {
static class WPFExceptionBuilder {
public static InvalidOperationException style_target_type(Type expected, Type actual) =>
new InvalidOperationException($"Expected Style.TargetType to be {expected}, actually {actual}.");
public static ArgumentException dp_target_type(Type expected, Type actual) =>
new ArgumentException($"The dependency property expects targets to be {expected}, actually {actual}.", "sender");
public static ArgumentException dp_target_type(Type expected, Type actual, DependencyProperty dp) =>
new ArgumentException($"The dependency property '{dp}' expects targets to be {expected}, actually {actual}.", "sender");
}
}
@@ -10,5 +10,11 @@ namespace mbts.ui {
} else if (style.TargetType != expected)
throw WPFExceptionBuilder.style_target_type(expected, style.TargetType);
}
public static T assert_dp_target_type<T>(DependencyObject target) where T : DependencyObject =>
target as T ?? throw WPFExceptionBuilder.dp_target_type(typeof(T), target?.GetType());
public static T assert_dp_target_type<T>(DependencyObject target, DependencyProperty dp) where T : DependencyObject =>
target as T ?? throw WPFExceptionBuilder.dp_target_type(typeof(T), target?.GetType(), dp);
}
}
@@ -0,0 +1,47 @@
// shamelessly copied from: https://serialseb.com/blog/2007/09/03/wpf-tips-6-preventing-scrollviewer-from/
using bts.utils;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace mbts.ui {
class ScrollViewerCorrector {
static List<MouseWheelEventArgs> _reentrancy_list => ThreadStaticActivator<ReentrancyList>.instance;
public static readonly DependencyProperty FixNestedScrollingProperty =
DependencyProperty.RegisterAttached("FixNestedScrolling", typeof(bool), typeof(ScrollViewerCorrector),
new PropertyMetadata(false, (d, e) => {
var target = WPFExceptionHelper.assert_dp_target_type<ScrollViewer>(d);
if ((bool)e.NewValue == true)
target.PreviewMouseWheel += _on_preview_mouse_wheel;
else if ((bool)e.NewValue == false)
target.PreviewMouseWheel -= _on_preview_mouse_wheel;
}));
public static bool GetFixNestedScrolling(DependencyObject d) => (bool)d.GetValue(FixNestedScrollingProperty);
public static void SetFixNestedScrolling(DependencyObject d, bool value) => d.SetValue(FixNestedScrollingProperty, value);
static void _on_preview_mouse_wheel(object sender, MouseWheelEventArgs e) {
if (!e.Handled && sender is ScrollViewer sv && !_reentrancy_list.Contains(e)) {
var preview_event_arg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta) {
RoutedEvent = UIElement.PreviewMouseWheelEvent,
Source = sender
};
_reentrancy_list.Add(preview_event_arg);
((IInputElement)e.OriginalSource).RaiseEvent(preview_event_arg);
_reentrancy_list.Remove(preview_event_arg);
// at this point if no one else handled the event in our children, we do our job
if (!preview_event_arg.Handled && ((e.Delta > 0 && sv.VerticalOffset == 0) || (e.Delta <= 0 && sv.VerticalOffset >= sv.ExtentHeight - sv.ViewportHeight))) {
e.Handled = true;
(sv.Parent as IInputElement)?.RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta) {
RoutedEvent = UIElement.MouseWheelEvent,
Source = sender
});
}
}
}
class ReentrancyList : List<MouseWheelEventArgs> { }
}
}