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);
        }
    



}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章