wpf ScrollViewer 滚动动画
wpf ScrollViewer 滚动动画:
<Window x:Class="WpfTest.FloatTextWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="FloatTextWindow" Height="450" Width="800">
<Grid>
<ScrollViewer x:Name="scrollView" VerticalScrollBarVisibility="Auto">
<StackPanel Height="1000">
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
<Button Background="Red" Height="55" Width="222">ghjghjhj</Button>
</StackPanel>
</ScrollViewer>
<Button Content="Scroll to Bottom" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfTest
{
/// <summary>
/// FloatTextWindow.xaml 的交互逻辑
/// </summary>
public partial class FloatTextWindow : Window
{
public FloatTextWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 计算滚动的目标位置
double targetVerticalOffset = scrollView.ExtentHeight - scrollView.ViewportHeight;
// 创建Storyboard和DoubleAnimation
Storyboard storyboard = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0;// scrollView.VerticalOffset;
animation.To = targetVerticalOffset;
animation.Duration = new Duration(TimeSpan.FromSeconds(8.5));
animation.AutoReverse = true;
animation.RepeatBehavior = RepeatBehavior.Forever;
// 指定动画的目标对象和属性
Storyboard.SetTarget(animation, scrollView);
Storyboard.SetTargetProperty(animation, new PropertyPath(ScrollViewerBehavior.VerticalOffsetProperty));
// 启动动画
storyboard.Children.Add(animation);
storyboard.Begin(this);
}
}
public static class ScrollViewerBehavior
{
public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new UIPropertyMetadata(0.0, OnVerticalOffsetChanged));
public static void SetVerticalOffset(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);
}
}
fffffffffffffffff
test red font.

浙公网安备 33010602011771号