wpf - Observable Collection 没有更新数据网格

标签 wpf vb.net observablecollection

我正在使用 Dim All_PriceLists As System.Collections.ObjectModel.ObservableCollection(Of BSPLib.PriceLists.PriceListPrime) 其中 PriceListPrime 为其中的所有属性实现 Inotify。

我将 All_PriceList 作为 DataGrid1.ItemsSource = All_PriceLists 绑定(bind)到数据网格,但是当我执行 All_PriceLists=Getall() 时,Getall 读取和从数据库中获取数据,数据网格没有更新。

只有当我以这种方式破解它时它才会更新:

DataGrid1.ItemsSource = Nothing
DataGrid1.ItemsSource = All_PriceLists

能否请您告诉我哪里出了问题或者我应该实现什么。谢谢。

最佳答案

您的问题有多种解决方案

  • 直接更新ItemsSource(而不是替换本地成员变量)

    DataGrid1.ItemsSource = new ObservableCollection(Of PriceListPrime)(GetAll())
    
  • 更新 ObservableCollection(如另一个答案中所述)

    All_PriceList.Clear(); 
    For Each item in Getall() 
        All_PriceList.Add(item) 
    Next 
    
  • 将 DataContext 设置为 View 模型并绑定(bind)到 View 模型的属性

    Dim vm as new MyViewModel()
    DataContext = vm
    vm.Items = new ObservableCollection(Of PriceListPrime)(GetAll())        
    

    View 模型将实现 INotifyPropertyChanged 并在 Items 属性更改时引发 PropertyChanged 事件。在 Xaml 中,您的 DataGrid 的 ItemsSource 将绑定(bind)到 Items 属性。

关于wpf - Observable Collection 没有更新数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472085/

相关文章:

wpf - 检测FlowDocument更改并滚动

wpf - 获取数据模板中的数据项

.net - 如何在字符串中包含 "

c# - 如何使用WPF在MVVM模式中的集合属性上实现IsDirty?

c# - LogicalTreeHelper.GetChildren - ObservableCollection Move() 导致 DataTemplate 中的 ContentControl 丢失其内容?

wpf - 错误 "Specified element is already the logical child of another element"?

c# - 绑定(bind)工作正常,但智能感知显示 : Cannot resolve property XXX in data context of type 'object'

vb.net - 使用 Math.NET 库在 vb.net 中绘制多项式

.net - MVVM 设计考虑

c# - MVVM:加载大数据记录