为 WPF 的 ScrollViewer 添加滚动动画

在 WPF 中,ScrollViewer 默认没有直接支持滚动动画的属性。不过,我们可以通过自定义附加属性和动画来实现滚动动画效果。

定义附加属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

public static class ScrollViewerBehavior
{
public static readonly DependencyProperty HorizontalOffsetProperty =
DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerBehavior),
new UIPropertyMetadata(0.0, OnHorizontalOffsetChanged));

public static void SetHorizontalOffset(FrameworkElement target, double value) =>
target.SetValue(HorizontalOffsetProperty, value);

public static double GetHorizontalOffset(FrameworkElement target) =>
(double)target.GetValue(HorizontalOffsetProperty);

private static void OnHorizontalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) =>
(target as ScrollViewer)?.ScrollToHorizontalOffset((double)e.NewValue);

public static readonly DependencyProperty VerticalOffsetProperty =
DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior),
new UIPropertyMetadata(0.0, OnVerticalOffsetChanged));

public static void VerticalOffset(FrameworkElement target, double value) =>
target.SetValue(VerticalOffsetProperty, value);

public static double GetVerticalOffset(FrameworkElement target) =>
(double)target.GetValue(VerticalOffsetProperty);

private static void OnVerticalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) =>
(target as ScrollViewer)?.ScrollToVerticalOffset((double)e.NewValue);

}

设置动画:

1
2
3
4
5
6
7
8
9
10
<Storyboard x:Key="ScrollStoryboard">
<DoubleAnimation Storyboard.TargetName="ScrollViewer"
Storyboard.TargetProperty="(YourNamespace:ScrollViewerBehavior.HorizontalOffset)"
From="0" To="500" Duration="0:0:0.6">
<DoubleAnimation.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>

为 WPF 的 ScrollViewer 添加滚动动画

http://example.com/2024/08/01/wpf-scrollviewer-animation/

Author

swety2003

Posted on

2024-08-01

Updated on

2025-10-06

Licensed under