简体   繁体   中英

SilverLight Datagrid refresh

I have a Silverlight Datagrid who's DataSource I refresh every 5 seconds. I would like when the grid refreshes, for the focus to be on the last row, not the first. I have tried setting the SelectedIndex property of the grid to be the last row, but it did not work.

The details:

I am binding the DataGrid to an ObservalbleList(Of MyObject) property on it's ViewModel, and the SelectedIndex is also a property on the ViewModel. Both properties raise the property changed event (able to witness this working by seeing the DataGrids DataSource clearly changing, but the SelectedIndex is never set.

When Googling the problem, I have read reports that setting the SelectedIndex on a DataGrid is a known issue, but have not found a work around. Any Ideas?

In your view model create a property for the CurrentItem/Entity like this:

    private Customer customer;
    public Customer CurrentCustomer
    {
        get { return this.customer; }
        set
        {
            if (this.customer!= value)
            {
                this.customer= value;
                OnPropertyChanged("CurrentCustomer");
            }
        }
    }

After you load up all your customers in your view model set CurrentCustomer to something like:

CurrentCustomer = context.Customers.Last();

In your View/XAML bind the datagrid's selected item to CurrentCustomer like:

SelectedItem="{Binding CurrentCustomer, Mode=TwoWay}"

Just reset CurrentCustomer like above after each 5-second refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM