BradleyUffner

scrollviewer fix

Aug 24th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1.     public class BubbleMouseWheelEvents : AttachableForStyleBehavior<UIElement, BubbleMouseWheelEvents>
  2.     {
  3.         protected override void OnAttached()
  4.         {
  5.             AssociatedObject.PreviewMouseWheel += PreviewMouseWheel;
  6.         }
  7.  
  8.         protected override void OnDetaching()
  9.         {
  10.             AssociatedObject.PreviewMouseWheel -= PreviewMouseWheel;
  11.         }
  12.  
  13.         private void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  14.         {
  15.             var scrollViewer = AssociatedObject.GetChildOf<ScrollViewer>(includeSelf: true);
  16.             var scrollPos = scrollViewer.ContentVerticalOffset;
  17.             if ((scrollPos == scrollViewer.ScrollableHeight && e.Delta < 0) || (scrollPos == 0 && e.Delta > 0))
  18.             {
  19.                 UIElement rerouteTo = AssociatedObject;
  20.                 if (ReferenceEquals(scrollViewer, AssociatedObject))
  21.                 {
  22.                     rerouteTo = (UIElement)VisualTreeHelper.GetParent(AssociatedObject);
  23.                 }
  24.  
  25.                 e.Handled = true;
  26.                 var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
  27.                 e2.RoutedEvent = UIElement.MouseWheelEvent;
  28.                 rerouteTo.RaiseEvent(e2);
  29.             }
  30.         }
  31.     }
  32.  
  33.     public class AttachableForStyleBehavior<TComponent, TBehavior> : Behavior<TComponent>
  34.         where TComponent : System.Windows.DependencyObject
  35.         where TBehavior : AttachableForStyleBehavior<TComponent, TBehavior>, new()
  36.     {
  37.         public static DependencyProperty IsEnabledForStyleProperty =
  38.             DependencyProperty.RegisterAttached("IsEnabledForStyle", typeof(bool),
  39.                                                 typeof(AttachableForStyleBehavior<TComponent, TBehavior>), new FrameworkPropertyMetadata(false, OnIsEnabledForStyleChanged));
  40.  
  41.         public bool IsEnabledForStyle
  42.         {
  43.             get { return (bool)GetValue(IsEnabledForStyleProperty); }
  44.             set { SetValue(IsEnabledForStyleProperty, value); }
  45.         }
  46.  
  47.         private static void OnIsEnabledForStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  48.         {
  49.             UIElement uie = d as UIElement;
  50.  
  51.             if (uie != null)
  52.             {
  53.                 var behColl = Interaction.GetBehaviors(uie);
  54.                 var existingBehavior = behColl.FirstOrDefault(b => b.GetType() ==
  55.                                                                    typeof(TBehavior)) as TBehavior;
  56.  
  57.                 if ((bool)e.NewValue == false && existingBehavior != null)
  58.                 {
  59.                     behColl.Remove(existingBehavior);
  60.                 }
  61.  
  62.                 else if ((bool)e.NewValue == true && existingBehavior == null)
  63.                 {
  64.                     behColl.Add(new TBehavior());
  65.                 }
  66.             }
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment