Wpf 模型-视图模型同步和过滤的ObservableCollections
我正在与EF和MVVM合作。 我的模型包含以下内容:Wpf 模型-视图模型同步和过滤的ObservableCollections,wpf,vb.net,mvvm,filtering,observablecollection,Wpf,Vb.net,Mvvm,Filtering,Observablecollection,我正在与EF和MVVM合作。 我的模型包含以下内容: Public Property Visits As DbSet(Of Visit) Visit类实现INotifyPropertyChanged,其属性正确引发PropertyChanged事件 My ViewModel包含以下属性,用于My views绑定上下文是My model: Private _BareVisits As ObservableCollection(Of Visit) Public Property BareV
Public Property Visits As DbSet(Of Visit)
Visit类实现INotifyPropertyChanged,其属性正确引发PropertyChanged事件
My ViewModel包含以下属性,用于My views绑定上下文是My model:
Private _BareVisits As ObservableCollection(Of Visit)
Public Property BareVisits As ObservableCollection(Of Visit)
Get
If _BareVisits Is Nothing Then
_BareVisits = New ObservableCollection(Of Visit)(context.Visits)
AddHandler _BareVisits.CollectionChanged, AddressOf BareVisits_CollectionChanged
End If
Return _BareVisits
End Get
Set(value As ObservableCollection(Of Visit))
_BareVisits = value
End Set
End Property
Private _Visits As ObservableCollection(Of Visit)
Public Property Visits As ObservableCollection(Of Visit)
Get
If _Visits Is Nothing Then
_Visits = New ObservableCollection(Of Visit)(
BareVisits.Where(
Function(V) Not V.IsMediGenerated AndAlso V.IsHistoryParseComplete
)
)
End If
Return _Visits
End Get
Set(value As ObservableCollection(Of Visit))
_Visits = value
End Set
End Property
Private Sub BareVisits_CollectionChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
Select Case e.Action
Case NotifyCollectionChangedAction.Add
context.Visits.AddRange(e.NewItems.OfType(Of Visit))
context.SaveChanges()
Case NotifyCollectionChangedAction.Remove
context.Visits.RemoveRange(e.OldItems.OfType(Of Visit))
context.SaveChanges()
Case Else
Exit Sub
End Select
End Sub
如您所见,BareVisites属性只是我访问的DB集合
我的视图绑定的访问属性依赖于返回过滤记录的访问
问题:
1保存新访问的最佳方法是什么?通过使用发布的事件处理程序?或者我应该直接与模型对话,并以某种方式将这些更改冒泡到ViewModel?如果我将CollectionChanged处理程序挂接到Visits属性,我是否需要这个BareVisits属性
2假设更改发生在模型本身,我想我也应该对BareVisites属性执行记录添加/删除。你的房子怎么了?过滤后的一个我已经尝试了一切,它的CollectionChanged事件从未被触发。我也应该用手来修改它吗
我只是想用我的视图来描述数据的变化,我感到不知所措
编辑:我还尝试使用ICollectionView来过滤原始数据,而不是新的ObservableCollection属性,但这不是一个选择,因为我的datagrid中的数据以这种方式呈现为不可编辑
先谢谢你。任何帮助都将不胜感激。取消私人访问,并在需要时重新确认。在添加/删除访问时,或作为事件处理程序的最后手段,执行NotifPropChanged。两个setter都不应使用,请将它们删除以明确说明。如果我删除此访问,则使属性为只读,getter将被转换为:`returnnewobserveCollectionofvisit.Where FunctionV Not V.IsMediGenerated and also V.IsHistoryParseComplete`observeCollection中的这个结果不会被一次又一次地重建吗?你能解释一下吗?是的,每次通知后都会重建。使用您从未计划绑定到的oberservable集合会消耗资源。