我正在使用 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/