user626528
user626528

Reputation: 14417

How to scroll DataGrid continuously?

I need to scroll DataGrid continuously, so it will move to next row each second (for example), and move to the first item when end is reached. Plz suggest the best way for this task.

Upvotes: 2

Views: 602

Answers (1)

TerenceJackson
TerenceJackson

Reputation: 1786

you could use a Dispatcher and each second you count up the selected index. Something like this:

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

EDIT

The selected index is set in code behind here, you can also bind it and do the ScrollIntoView stuff in the selection changed handler.

BR,

TJ

Upvotes: 3

Related Questions