终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

112 rader
4.0 KiB

  1. using BPASmartClient.MilkWithTea.Data;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace BPASmartClient.MilkWithTea.Control;
  8. public class ScrollViewerAttach
  9. {
  10. public static readonly DependencyProperty AutoHideProperty = DependencyProperty.RegisterAttached(
  11. "AutoHide", typeof(bool), typeof(ScrollViewerAttach), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
  12. public static void SetAutoHide(DependencyObject element, bool value)
  13. => element.SetValue(AutoHideProperty, ValueBoxes.BooleanBox(value));
  14. public static bool GetAutoHide(DependencyObject element)
  15. => (bool) element.GetValue(AutoHideProperty);
  16. public static readonly DependencyProperty OrientationProperty = DependencyProperty.RegisterAttached(
  17. "Orientation", typeof(Orientation), typeof(ScrollViewerAttach), new FrameworkPropertyMetadata(ValueBoxes.VerticalBox, FrameworkPropertyMetadataOptions.Inherits, OnOrientationChanged));
  18. private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  19. {
  20. if (d is ScrollViewer)
  21. {
  22. return;
  23. }
  24. if (d is System.Windows.Controls.ScrollViewer scrollViewer)
  25. {
  26. if ((Orientation) e.NewValue == Orientation.Horizontal)
  27. {
  28. scrollViewer.PreviewMouseWheel += ScrollViewerPreviewMouseWheel;
  29. }
  30. else
  31. {
  32. scrollViewer.PreviewMouseWheel -= ScrollViewerPreviewMouseWheel;
  33. }
  34. }
  35. void ScrollViewerPreviewMouseWheel(object sender, MouseWheelEventArgs args)
  36. {
  37. var scrollViewerNative = (System.Windows.Controls.ScrollViewer) sender;
  38. scrollViewerNative.ScrollToHorizontalOffset(Math.Min(Math.Max(0, scrollViewerNative.HorizontalOffset - args.Delta), scrollViewerNative.ScrollableWidth));
  39. args.Handled = true;
  40. }
  41. }
  42. public static void SetOrientation(DependencyObject element, Orientation value)
  43. => element.SetValue(OrientationProperty, ValueBoxes.OrientationBox(value));
  44. public static Orientation GetOrientation(DependencyObject element)
  45. => (Orientation) element.GetValue(OrientationProperty);
  46. public static readonly DependencyProperty IsDisabledProperty = DependencyProperty.RegisterAttached(
  47. "IsDisabled", typeof(bool), typeof(ScrollViewerAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDisabledChanged));
  48. private static void OnIsDisabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. {
  50. if (d is UIElement element)
  51. {
  52. if ((bool) e.NewValue)
  53. {
  54. element.PreviewMouseWheel += ScrollViewerPreviewMouseWheel;
  55. }
  56. else
  57. {
  58. element.PreviewMouseWheel -= ScrollViewerPreviewMouseWheel;
  59. }
  60. }
  61. void ScrollViewerPreviewMouseWheel(object sender, MouseWheelEventArgs args)
  62. {
  63. if (args.Handled)
  64. {
  65. return;
  66. }
  67. args.Handled = true;
  68. if (GetParent<System.Windows.Controls.ScrollViewer>((UIElement) sender) is { } scrollViewer)
  69. {
  70. scrollViewer.RaiseEvent(new MouseWheelEventArgs(args.MouseDevice, args.Timestamp, args.Delta)
  71. {
  72. RoutedEvent = UIElement.MouseWheelEvent,
  73. Source = sender
  74. });
  75. }
  76. }
  77. static T GetParent<T>(DependencyObject d) where T : DependencyObject =>
  78. d switch
  79. {
  80. null => default,
  81. T t => t,
  82. Window _ => null,
  83. _ => GetParent<T>(VisualTreeHelper.GetParent(d))
  84. };
  85. }
  86. public static void SetIsDisabled(DependencyObject element, bool value)
  87. => element.SetValue(IsDisabledProperty, ValueBoxes.BooleanBox(value));
  88. public static bool GetIsDisabled(DependencyObject element)
  89. => (bool) element.GetValue(IsDisabledProperty);
  90. }