GotoTop.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using HandyControl.Data;
  5. using HandyControl.Tools;
  6. using HandyControl.Tools.Extension;
  7. namespace HandyControl.Controls;
  8. public class GotoTop : Button
  9. {
  10. private Action _gotoTopAction;
  11. private System.Windows.Controls.ScrollViewer _scrollViewer;
  12. public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(
  13. nameof(Target), typeof(DependencyObject), typeof(GotoTop), new PropertyMetadata(default(DependencyObject)));
  14. public DependencyObject Target
  15. {
  16. get => (DependencyObject) GetValue(TargetProperty);
  17. set => SetValue(TargetProperty, value);
  18. }
  19. public GotoTop() => Loaded += (s, e) => CreateGotoAction(Target);
  20. public virtual void CreateGotoAction(DependencyObject obj)
  21. {
  22. if (_scrollViewer != null)
  23. {
  24. _scrollViewer.ScrollChanged -= ScrollViewer_ScrollChanged;
  25. }
  26. _scrollViewer = VisualHelper.GetChild<System.Windows.Controls.ScrollViewer>(obj);
  27. if (_scrollViewer != null)
  28. {
  29. _scrollViewer.ScrollChanged += ScrollViewer_ScrollChanged;
  30. if (_scrollViewer is ScrollViewer scrollViewerHandy && Animated && scrollViewerHandy.IsInertiaEnabled)
  31. {
  32. _gotoTopAction = () => scrollViewerHandy.ScrollToTopInternal(AnimationTime);
  33. }
  34. else
  35. {
  36. _gotoTopAction = () => _scrollViewer.ScrollToTop();
  37. }
  38. }
  39. }
  40. private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
  41. {
  42. if (AutoHiding)
  43. {
  44. this.Show(e.VerticalOffset >= HidingHeight);
  45. }
  46. }
  47. public static readonly DependencyProperty AnimatedProperty = DependencyProperty.Register(
  48. nameof(Animated), typeof(bool), typeof(GotoTop), new PropertyMetadata(ValueBoxes.TrueBox));
  49. public bool Animated
  50. {
  51. get => (bool) GetValue(AnimatedProperty);
  52. set => SetValue(AnimatedProperty, ValueBoxes.BooleanBox(value));
  53. }
  54. public static readonly DependencyProperty AnimationTimeProperty = DependencyProperty.Register(
  55. nameof(AnimationTime), typeof(double), typeof(GotoTop), new PropertyMetadata(ValueBoxes.Double200Box));
  56. public double AnimationTime
  57. {
  58. get => (double) GetValue(AnimationTimeProperty);
  59. set => SetValue(AnimationTimeProperty, value);
  60. }
  61. public static readonly DependencyProperty HidingHeightProperty = DependencyProperty.Register(
  62. nameof(HidingHeight), typeof(double), typeof(GotoTop), new PropertyMetadata(ValueBoxes.Double0Box));
  63. public double HidingHeight
  64. {
  65. get => (double) GetValue(HidingHeightProperty);
  66. set => SetValue(HidingHeightProperty, value);
  67. }
  68. public static readonly DependencyProperty AutoHidingProperty = DependencyProperty.Register(
  69. nameof(AutoHiding), typeof(bool), typeof(GotoTop), new PropertyMetadata(ValueBoxes.TrueBox));
  70. public bool AutoHiding
  71. {
  72. get => (bool) GetValue(AutoHidingProperty);
  73. set => SetValue(AutoHidingProperty, ValueBoxes.BooleanBox(value));
  74. }
  75. protected override void OnClick()
  76. {
  77. base.OnClick();
  78. _gotoTopAction?.Invoke();
  79. }
  80. }