弹来弹去跑马灯!

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
31
32
33
34
<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>

  

 

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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);
        }
     
 
 
 
}

  

posted @   wgscd  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
历史上的今天:
2021-07-28 WPF 设置Button的content为多行模式
2016-07-28 Advanced Find and Replace(文件内容搜索替换工具)v7.8.1简体中文破解版
点击右上角即可分享
微信分享提示