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

 

xml 代碼

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

 

動畫綁定,並開始動畫

  private Void ScrollViewerAnimation(int TotalCount)// TotalCount 共有多少條數據
  {
       int LineCount=5;//每行顯示5條數據
       Storyboard storyboard = new Storyboard(); 
       TimeSpan ts = new TimeSpan(0, 0, 25);//向下滾動耗時
            double end = 200;; //向下滾動距離,可滾動數據源個數調整  
            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; //ture:來回滾動,False:只向下滾動
            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;
        }

 

<Listbox X:Name="_Listbox"/>

  

      private void ListAnimation(int TotalCount)
        {
            scrollViewer = FindVisualChild<ScrollViewer>(this._Listbox);
         Storyboard storyboard = new Storyboard();
            TimeSpan ts = new TimeSpan(0, 2, 0);
            double end = 200;//滾動距離,可根據數據源個數調整
            DoubleAnimation TopToButtomAnimation = new DoubleAnimation();
            TopToButtomAnimation.EasingFunction = new SineEase { EasingMode = EasingMode.EaseOut };
            TopToButtomAnimation.From = 0;
            TopToButtomAnimation.To = end;
            TopToButtomAnimation.Duration = new Duration(ts);
            TopToButtomAnimation.AutoReverse = true;
            TopToButtomAnimation.SpeedRatio = 1;
            storyboard.Duration = new Duration(TimeSpan.FromMinutes(4));
            storyboard.Children.Add(TopToButtomAnimation);
            Storyboard.SetTarget(TopToButtomAnimation, scrollViewer);
            Storyboard.SetTargetName(TopToButtomAnimation, scrollViewer.Name);
            Storyboard.SetTargetProperty(TopToButtomAnimation, new PropertyPath(ScrollViewerBehavior.VerticalOffsetProperty));
            storyboard.RepeatBehavior = RepeatBehavior.Forever;
            storyboard.Begin();

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM