【问题标题】:updating datagrid with BindingList使用 BindingList 更新数据网格
【发布时间】:2014-09-09 20:58:20
【问题描述】:

我一直在研究 BindingList 与 observablecollection 和 List 之间的区别。从我读过的内容来看,似乎 BindingList 是唯一会通知其中的对象是否更改了其属性之一的集合类型。我无法让它工作。

我在 ViewModel 上有一个名为 Matches 的属性,它返回一个从另一个类中的 CarMatch 对象列表创建的 BindingList。 (Cars m_Cars = new Cars();) 我在 View 上的 DataGrid 绑定到 VM 中的这个 Matches 属性。

public BindingList<CarMatch> Matches
{ 
    get
    {
        Return new BindingList<CarMatch>(m_Cars.Matches);  
    }
}

现在,在代码中,我更改了 CarMatch 对象的属性之一,例如,从 false 到 autoTrans = true。匹配[0].automaticTrans = true。我想在 DataGrid 中看到这种变化。如果没有在 CarMatch 类中实现 INotifyPropertyChanged,有没有办法从视图模型更新数据网格?在 Matches 上使用 INotifyPropertyChanged 似乎并没有这样做。这个有点不明白,可以举个例子看看。

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    CarMatch(不是Matches)必须实现INotifyPropertyChanged。但是考虑使用ObservableCollection,除非你真的需要BindingList提供的一些额外场景:ObservableCollectionINotifyPropertyChanged是免费提供的。而且,更重要的是,BindingList doesn't scale well

    【讨论】:

    • 好的,我认为 BindingList 具有其他一些功能,如果它们的属性发生更改而列表对象的类中没有 INotifyPropertyChanged,它可以更新它的项目。
    【解决方案2】:

    试试

    dataGrid.Items.Refresh();
    

    但请记住,如果您有大量数据并且在短时间内调用多次,那将是一个昂贵的调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多