Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BubbleMouseWheelEvents : AttachableForStyleBehavior<UIElement, BubbleMouseWheelEvents>
- {
- protected override void OnAttached()
- {
- AssociatedObject.PreviewMouseWheel += PreviewMouseWheel;
- }
- protected override void OnDetaching()
- {
- AssociatedObject.PreviewMouseWheel -= PreviewMouseWheel;
- }
- private void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
- {
- var scrollViewer = AssociatedObject.GetChildOf<ScrollViewer>(includeSelf: true);
- var scrollPos = scrollViewer.ContentVerticalOffset;
- if ((scrollPos == scrollViewer.ScrollableHeight && e.Delta < 0) || (scrollPos == 0 && e.Delta > 0))
- {
- UIElement rerouteTo = AssociatedObject;
- if (ReferenceEquals(scrollViewer, AssociatedObject))
- {
- rerouteTo = (UIElement)VisualTreeHelper.GetParent(AssociatedObject);
- }
- e.Handled = true;
- var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
- e2.RoutedEvent = UIElement.MouseWheelEvent;
- rerouteTo.RaiseEvent(e2);
- }
- }
- }
- public class AttachableForStyleBehavior<TComponent, TBehavior> : Behavior<TComponent>
- where TComponent : System.Windows.DependencyObject
- where TBehavior : AttachableForStyleBehavior<TComponent, TBehavior>, new()
- {
- public static DependencyProperty IsEnabledForStyleProperty =
- DependencyProperty.RegisterAttached("IsEnabledForStyle", typeof(bool),
- typeof(AttachableForStyleBehavior<TComponent, TBehavior>), new FrameworkPropertyMetadata(false, OnIsEnabledForStyleChanged));
- public bool IsEnabledForStyle
- {
- get { return (bool)GetValue(IsEnabledForStyleProperty); }
- set { SetValue(IsEnabledForStyleProperty, value); }
- }
- private static void OnIsEnabledForStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- UIElement uie = d as UIElement;
- if (uie != null)
- {
- var behColl = Interaction.GetBehaviors(uie);
- var existingBehavior = behColl.FirstOrDefault(b => b.GetType() ==
- typeof(TBehavior)) as TBehavior;
- if ((bool)e.NewValue == false && existingBehavior != null)
- {
- behColl.Remove(existingBehavior);
- }
- else if ((bool)e.NewValue == true && existingBehavior == null)
- {
- behColl.Add(new TBehavior());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment