zoukankan      html  css  js  c++  java
  • Wpf自动滚动效果

    一、思路

    1.使用ScrollView的Scroll.ScrollToVerticalOffset(offset)方法进行滚动

    2.ScrollView中放置2个ListView,第一个滚动出边界后,移除,然后再动态添加一个内容相同的ListView

    3.ListView设置最小高度,以保证在内容不多时同一条内容出现2次

    4.每当ListView滚动出边界后,offset置0,放置offset无限增大,导致崩溃,同时方便计算ListView何时滚动出边界

    二、实现

    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.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    
    namespace WpfApp1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            double offset = 0.0;
            
            public MainWindow()
            {
                InitializeComponent();
                //设置定时器     
    
    
                ContentPanel.Children.Add(new MyListView());
                ContentPanel.Children.Add(new MyListView());
    
                for (int i = 0; i < 5; i++)
                {
                    CreateInt(i);             
                }
    
                timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(1000);   //时间间隔为一秒
                timer.Tick += new EventHandler(timer_Tick);      
                timer.Start();            
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                CreateRandom();
            }
    
            private void CreateRandom()
            {
                Random random = new Random();
                AddItem(random.Next().ToString());
            }
    
            private void CreateInt(int i)
            {
                AddItem(i.ToString());
            }
    
            private void AddItem(string content)
            {
                foreach (var lst in ContentPanel.Children)
                {
                    if (lst is MyListView)
                    {
                        (lst as MyListView).Items.Add(content);
                    }
                }
            }
    
            private DispatcherTimer timer;
    
            private void timer_Tick(object sender, EventArgs e)
            {
                offset += 0.01;
                Scroll.ScrollToVerticalOffset(offset);
                if (offset >= (ContentPanel.Children[0] as MyListView).ActualHeight)
                {
                    Console.WriteLine("添加新list");
                    MyListView myListView = new MyListView();
                    foreach (var item in (ContentPanel.Children[0] as MyListView).Items)
                    {
                        myListView.Items.Add(item);
                    }
                    ContentPanel.Children.RemoveAt(0);
                    ContentPanel.Children.Add(myListView);
                    offset = 0.0;
                }
            }
        }
    }

    三、其他

    MyListView只是一个设置了固定高度的ListView

  • 相关阅读:
    说说该死的Google Android Market
    由HTML5绘制地图说开去
    unicode解码小工具
    IntelliJ IDEA 的安装、配置与使用
    再次强调!考试必带的十几样物品,一样也不能少
    Redis 实现限流的三种方式
    Linux配置/etc/resolv.conf详解
    人生哲学
    一文教你如何高效使用 IDEA !
    Mysql5.7.30_配置参数
  • 原文地址:https://www.cnblogs.com/punkrocker/p/11261295.html
Copyright © 2011-2022 走看看