From ee8f18aaea11a5e6d8f0714f1c88642b6d33c968 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Tue, 21 Dec 2021 22:48:24 +0800
Subject: [PATCH] enhance: add Orientation prop for ScrollViewerAttach
---
.../HandyControl_Net_40/Themes/Theme.xaml | 2 +-
.../Controls/ScrollViewerDemoCtl.xaml | 2 +-
.../Controls/WaterfallPanelDemoCtl.xaml | 2 +-
.../Styles/NativeScrollViewerDemoCtl.xaml | 2 +-
.../Controls/Attach/ScrollViewerAttach.cs | 41 ++++++++++++++++++-
.../Controls/Other/ScrollViewer.cs | 19 +--------
.../HandyControl_Shared/Data/ValueBoxes.cs | 11 ++++-
.../Styles/Base/TabControlBaseStyle.xaml | 2 +-
.../HandyControl_Shared/Themes/Theme.xaml | 2 +-
9 files changed, 58 insertions(+), 25 deletions(-)
diff --git a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
index f1ce9662..a04e38c2 100644
--- a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
+++ b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml
@@ -10874,7 +10874,7 @@
-
+
diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/ScrollViewerDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/ScrollViewerDemoCtl.xaml
index 8f4ce600..08918f62 100644
--- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/ScrollViewerDemoCtl.xaml
+++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/ScrollViewerDemoCtl.xaml
@@ -14,7 +14,7 @@
-
+
diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/WaterfallPanelDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/WaterfallPanelDemoCtl.xaml
index 253b7707..37218e9a 100644
--- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/WaterfallPanelDemoCtl.xaml
+++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/WaterfallPanelDemoCtl.xaml
@@ -32,7 +32,7 @@
-
+
diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/NativeScrollViewerDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/NativeScrollViewerDemoCtl.xaml
index 2478bb37..b0447226 100644
--- a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/NativeScrollViewerDemoCtl.xaml
+++ b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/NativeScrollViewerDemoCtl.xaml
@@ -14,7 +14,7 @@
-
+
diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/ScrollViewerAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/ScrollViewerAttach.cs
index 40c1df2f..f78e4545 100644
--- a/src/Shared/HandyControl_Shared/Controls/Attach/ScrollViewerAttach.cs
+++ b/src/Shared/HandyControl_Shared/Controls/Attach/ScrollViewerAttach.cs
@@ -1,4 +1,6 @@
-using System.Windows;
+using System;
+using System.Windows;
+using System.Windows.Controls;
using HandyControl.Data;
namespace HandyControl.Controls
@@ -13,5 +15,42 @@ namespace HandyControl.Controls
public static bool GetAutoHide(DependencyObject element)
=> (bool) element.GetValue(AutoHideProperty);
+
+ public static readonly DependencyProperty OrientationProperty = DependencyProperty.RegisterAttached(
+ "Orientation", typeof(Orientation), typeof(ScrollViewerAttach), new FrameworkPropertyMetadata(ValueBoxes.VerticalBox, FrameworkPropertyMetadataOptions.Inherits, OnOrientationChanged));
+
+ private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is ScrollViewer)
+ {
+ return;
+ }
+
+ if (d is System.Windows.Controls.ScrollViewer scrollViewer)
+ {
+ if ((Orientation) e.NewValue == Orientation.Horizontal)
+ {
+ scrollViewer.PreviewMouseWheel += ScrollViewer_PreviewMouseWheel;
+ }
+ else
+ {
+ scrollViewer.PreviewMouseWheel -= ScrollViewer_PreviewMouseWheel;
+ }
+ }
+ }
+
+ private static void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
+ {
+ var scrollViewer = (System.Windows.Controls.ScrollViewer) sender;
+ scrollViewer.ScrollToHorizontalOffset(Math.Min(Math.Max(0, scrollViewer.HorizontalOffset - e.Delta), scrollViewer.ScrollableWidth));
+
+ e.Handled = true;
+ }
+
+ public static void SetOrientation(DependencyObject element, Orientation value)
+ => element.SetValue(OrientationProperty, ValueBoxes.OrientationBox(value));
+
+ public static Orientation GetOrientation(DependencyObject element)
+ => (Orientation) element.GetValue(OrientationProperty);
}
}
diff --git a/src/Shared/HandyControl_Shared/Controls/Other/ScrollViewer.cs b/src/Shared/HandyControl_Shared/Controls/Other/ScrollViewer.cs
index 6cc7cf4e..9b0e794d 100644
--- a/src/Shared/HandyControl_Shared/Controls/Other/ScrollViewer.cs
+++ b/src/Shared/HandyControl_Shared/Controls/Other/ScrollViewer.cs
@@ -17,21 +17,6 @@ namespace HandyControl.Controls
private bool _isRunning;
- ///
- /// 滚动方向
- ///
- public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
- "Orientation", typeof(Orientation), typeof(ScrollViewer), new PropertyMetadata(Orientation.Vertical));
-
- ///
- /// 滚动方向
- ///
- public Orientation Orientation
- {
- get => (Orientation) GetValue(OrientationProperty);
- set => SetValue(OrientationProperty, value);
- }
-
///
/// 是否响应鼠标滚轮操作
///
@@ -53,7 +38,7 @@ namespace HandyControl.Controls
if (!IsInertiaEnabled)
{
- if (Orientation == Orientation.Vertical)
+ if (ScrollViewerAttach.GetOrientation(this) == Orientation.Vertical)
{
base.OnMouseWheel(e);
}
@@ -68,7 +53,7 @@ namespace HandyControl.Controls
}
e.Handled = true;
- if (Orientation == Orientation.Vertical)
+ if (ScrollViewerAttach.GetOrientation(this) == Orientation.Vertical)
{
if (!_isRunning)
{
diff --git a/src/Shared/HandyControl_Shared/Data/ValueBoxes.cs b/src/Shared/HandyControl_Shared/Data/ValueBoxes.cs
index 42ad020b..8c25b761 100644
--- a/src/Shared/HandyControl_Shared/Data/ValueBoxes.cs
+++ b/src/Shared/HandyControl_Shared/Data/ValueBoxes.cs
@@ -1,4 +1,6 @@
-namespace HandyControl.Data
+using System.Windows.Controls;
+
+namespace HandyControl.Data
{
///
/// 装箱后的值类型(用于提高效率)
@@ -9,6 +11,10 @@
internal static object FalseBox = false;
+ internal static object VerticalBox = Orientation.Vertical;
+
+ internal static object HorizontalBox = Orientation.Horizontal;
+
internal static object Double0Box = .0;
internal static object Double01Box = .1;
@@ -38,5 +44,8 @@
internal static object Int99Box = 99;
internal static object BooleanBox(bool value) => value ? TrueBox : FalseBox;
+
+ internal static object OrientationBox(Orientation value) =>
+ value == Orientation.Horizontal ? HorizontalBox : VerticalBox;
}
}
diff --git a/src/Shared/HandyControl_Shared/Themes/Styles/Base/TabControlBaseStyle.xaml b/src/Shared/HandyControl_Shared/Themes/Styles/Base/TabControlBaseStyle.xaml
index 295213ee..a7db8ae9 100644
--- a/src/Shared/HandyControl_Shared/Themes/Styles/Base/TabControlBaseStyle.xaml
+++ b/src/Shared/HandyControl_Shared/Themes/Styles/Base/TabControlBaseStyle.xaml
@@ -291,7 +291,7 @@
-
+
diff --git a/src/Shared/HandyControl_Shared/Themes/Theme.xaml b/src/Shared/HandyControl_Shared/Themes/Theme.xaml
index cf31f075..2945a9a8 100644
--- a/src/Shared/HandyControl_Shared/Themes/Theme.xaml
+++ b/src/Shared/HandyControl_Shared/Themes/Theme.xaml
@@ -10992,7 +10992,7 @@
-
+