【问题标题】:How to scroll DataGrid continuously?如何连续滚动 DataGrid?
【发布时间】:2011-02-22 10:25:27
【问题描述】:

我需要连续滚动 DataGrid,所以它会每秒移动到下一行(例如),并在到达 end 时移动到第一项。请建议完成这项任务的最佳方法。

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    您可以使用 Dispatcher 并每秒计算所选索引。 像这样的:

      private int selectedIndex;
                public int SelectedIndex
                {
                    get { return selectedIndex; }
                    set
                    {
                        selectedIndex = value;
                        NotifyPropertyChanged("SelectedIndex");
                    }
                }
    
                private void BuildDispatcher()
                {
                    DispatcherTimer dispatcherTimer = new DispatcherTimer();
                    dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
                    dispatcherTimer.Tick += DispatcherTimerTick;
                    dispatcherTimer.Start();
                }
    
                void DispatcherTimerTick(object sender, EventArgs e)
                {
                    if((SelectedIndex + 1) > MyCollection.Count)
                    {
                        SelectedIndex = 0;
                    }else
                    {
                        SelectedIndex++;
                    }
    //EDIT!
                   MyDataGrid.SelectedIndex = SelectedIndex;
                    MyDataGrid.ScrollIntoView(MyDataGrid.SelectedItem, MyDataGrid.Columns[0]);
                }
    

    编辑

    选择的索引在这里后面的代码中设置,你也可以绑定它并在选择更改处理程序中做 ScrollIntoView 的东西。

    BR,

    TJ

    【讨论】:

    • 它不起作用。选择项更改,但网格不滚动。
    • 嗨,你是对的。请参阅我的编辑。您需要添加 ScrollIntoView 来完成它。 MyDataGrid 是保存数据的网格。如果要绑定它,可以处理选择更改事件,然后使用 ScrollIntoView。这能解决您的问题吗?
    • 还是不好的解决方案 :) 在所选项目到达第一页末尾之前有很大的延迟,然后才开始滚动。
    猜你喜欢
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2021-05-14
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多