【问题标题】:SilverLight Datagrid refreshSilverLight 数据网格刷新
【发布时间】:2011-01-18 11:22:44
【问题描述】:

我有一个 Silverlight Datagrid,它是我每 5 秒刷新一次的 DataSource。 我想当网格刷新时,焦点放在最后一行,而不是第一行。 我尝试将网格的 SelectedIndex 属性设置为最后一行,但没有成功。

详情:

我将 DataGrid 绑定到其 ViewModel 上的 ObservalbleList(Of MyObject) 属性,并且 SelectedIndex 也是 ViewModel 上的属性。两个属性都会引发属性更改事件(能够通过看到 DataGrids 数据源明显更改来见证此工作,但从未设置 SelectedIndex。

在谷歌搜索问题时,我已阅读报告称在 DataGrid 上设置 SelectedIndex 是一个已知问题,但尚未找到解决方法。有什么想法吗?

【问题讨论】:

    标签: c# .net vb.net silverlight silverlight-4.0


    【解决方案1】:

    在您的视图模型中为 CurrentItem/Entity 创建一个属性,如下所示:

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

    在视图模型中加载所有客户后,将 CurrentCustomer 设置为:

    CurrentCustomer = context.Customers.Last();
    

    在您的视图/XAML 中,将数据网格的选定 item 绑定到 CurrentCustomer,例如:

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

    每刷新 5 秒后,只需像上面一样重置 CurrentCustomer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2011-07-26
      相关资源
      最近更新 更多