enhance: add Orientation prop for ScrollViewerAttach
This commit is contained in:
1 parent
3db85c35f8
commit
ee8f18aaea
9 files changed
+58
-25
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -17,21 +17,6 @@ namespace HandyControl.Controls
|
||||
|
||||
private bool _isRunning;
|
||||
|
||||
/// <summary>
|
||||
/// 滚动方向
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
|
||||
"Orientation", typeof(Orientation), typeof(ScrollViewer), new PropertyMetadata(Orientation.Vertical));
|
||||
|
||||
/// <summary>
|
||||
/// 滚动方向
|
||||
/// </summary>
|
||||
public Orientation Orientation
|
||||
{
|
||||
get => (Orientation) GetValue(OrientationProperty);
|
||||
set => SetValue(OrientationProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否响应鼠标滚轮操作
|
||||
/// </summary>
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user