微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WPF ScrollViewer 内容滚动

 

采用属性动画的方式。由于动画绑定时需要绑定依赖属性,由于ScrollViewer没有水平偏移和垂直偏移的依赖属性,所以需要通过附加属性的方式添加水平和垂直的依赖属性

 

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

 

xml 代码

    
 <ScrollViewer HorizontalScrollBarVisibility="disabled" Grid.Column="1" VerticalScrollBarVisibility="Hidden"  
                          HorizontalAlignment="Stretch" x:Name="ScrollViewertest"  Margin="10,0" >
<!--填充控件-->
            
</ScrollViewer>

 

动画绑定,并开始动画

   TimeSpan ts = new TimeSpan(0,0,25);//向下滚动耗时
            double end = 20; //向下滚动距离    
            DoubleAnimation TopToButtomAnimation = new DoubleAnimation();
            TopToButtomAnimation.EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut };//EasingMode:滚动方式
            TopToButtomAnimation.From = 0;
            TopToButtomAnimation.To = end;
            TopToButtomAnimation.Duration = new Duration(ts);
            TopToButtomAnimation.AutoReverse = true;
            TopToButtomAnimation.Speedratio = 1;//滚动速率
            storyboard.Duration = new Duration(TimeSpan.FromSeconds(50));//上下滚动总时长
            storyboard.Children.Add(TopToButtomAnimation);
            Storyboard.SetTarget(TopToButtomAnimation,ScrollViewertest);
            Storyboard.SetTargetName(TopToButtomAnimation,ScrollViewertest.Name);
            Storyboard.SetTargetProperty(TopToButtomAnimation,new PropertyPath(ScrollViewerBehavior.VerticalOffsetProperty));
            storyboard.RepeatBehavior = RepeatBehavior.Forever;
            storyboard.FillBehavior = FillBehavior.HoldEnd;
            storyboard.Begin();

List和DataGrid滚动都可以采取该方式,控制控件的ScrollViewer 进行滚动。

        private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj,i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。