首页 > 其他分享 >wpf ScrollViewer 滚动动画

wpf ScrollViewer 滚动动画

时间:2023-07-28 09:35:30浏览次数:28  
标签:动画 ScrollViewer ghjghjhj double System Windows animation using wpf

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



}

  

标签:动画,ScrollViewer,ghjghjhj,double,System,Windows,animation,using,wpf
From: https://www.cnblogs.com/wgscd/p/17586734.html

相关文章

  • 2023-7-27 WPF自定义命名空间在xaml中的使用
    xaml自定义命名空间【作者】长生为啥要用自定义命名空间这是常见的几种命名空间xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:Rxsfadsf"xmlns:s......
  • 2023-7-27WPF的ContextMenu的传参绑定方式
    WPF的ContextMenu的绑定方式【作者】长生ContextMenu为何不能正常绑定在wpf中ContextMenu和ToolTip一样都是弹出层,与VisualTree已经分离了,只不过ToolTip在wpf中有进行特殊处理,所以可以正常绑定。个人觉得ContextMenu绑定的最可靠的方式首先添加BindingProxy类,继承Freezab......
  • WPF Pack URI路径访问二进制资源
    wpf在使用第三方框架时,引入资源文件有时候会遇到二进制放上引入,这里复习一下PackURI方式访问二进制文件资源二进制文件如图片文件,通过添加的方法已经添加到我们的程序中了,那么怎么访问到它们呢?wpf对二进制资源的访问有自己的一套方法,称为packURI路径。格式:pack://......
  • WPF 在ScrollViewer控件内部的Slider控件无法触摸滑动
    WPF中在ScrollViewer控件内部的Slider控件无法触摸滑动,是由于ScrollViewer控件默认设置了IsManipulationEnabled为True。<ScrollViewerx:Name="ScrollViewer1"><Slider></Slider></ScrollViewer>WPFScrollViewer的IsManipulationE......
  • WPF画导航箭头,始终指向鼠标位置
     界面:<Canvasx:Name="container"><Viewboxx:Name="player"Width="50"Height="50"RenderTransformOrigin="0.5,0.5"><Viewbox.RenderTransform><TransformGroup>......
  • jquery 边框动画
    jQuery边框动画在网页开发中,经常会用到动画效果来增强用户体验。其中,边框动画是一种常见的效果,可以为网页元素添加动态的边框样式,使其在页面上更加突出和吸引人。而使用jQuery,我们可以轻松实现各种边框动画效果。什么是jQueryjQuery是一个快速、简洁的JavaScript库,是目前......
  • 用CSS样式 @keyframes、animation写一个旋转立体模型、动画模型,vue2
    需求:画一个正方体,让物体一直旋转环境:vue2、css效果:代码:模型1<template>2<div>3<!--旋转立体图-->4<divclass="cube">5<divclass="facefront"></div>6<divclass="faceba......
  • WPF学习——开篇
    不知不觉我学习WPF已经两年啦。大部分时间是自娱自乐,只做了一个公司的小项目,有一点不能否认,WPF是我用过的最好的GUI框架。用过WPF之后,Qt,MFC,Duilib什么的碰都不想碰一下,Qt和MFC是差不多档次的大型框架,Duilib是库级别的东西,完全不是一个档次的。当然了,客户端还有新贵electron,但目前......
  • DevExpress WPF Tree List组件,让数据可视化程度更高!(一)
    DevExpressWPFTreeList组件是一个功能齐全、数据感知的TreeView-ListView混合体,可以把数据信息显示为REE、GRID或两者的组合,在数据绑定或非绑定模式下,具有完整的数据编辑支持。DevExpressWPF 拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过......
  • 如何删除PPT中工具栏口袋动画
    口袋动画官网无法打开http://www.papocket.com/插件无法使用卸载在【程序和功能】中卸载后,打开PPT,菜单还是存在选项——加载项,点击以p开头的一串代码(com加载项),点击转到,选择两个以p开头的加载项,依次删除即可注意不要把其他的加载项删了......