开发者

SilverLight Datagrid refresh

开发者 https://www.devze.com 2023-02-05 02:03 出处:网络
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 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 deta开发者_如何学Pythonils:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消