说,我有一个ObservableCollection
Class Person
Name
Event DidSomething
Class House
WithEvents People as ObservableCollection(of Person)
Sub OnPersonDidSomething(p as Person)
Console.WriteLine("{0} did something", p.Name)
现在,我应该如何实现此ObservableCollection的处理程序?
Private Sub People_CollectionChanged( _
sender As Object, _
e As NotifyCollectionChangedEventArgs) Handles People.CollectionChanged
Select Case e.Action
Case Specialized.NotifyCollectionChangedAction.Add
For Each mp As Person In e.NewItems
RemoveHandler mp.DidSomething, AddressOf OnPersonDidSomething
AddHandler mp.DidSomething, AddressOf OnPersonDidSomething
Next mp
Case Specialized.NotifyCollectionChangedAction.Remove
For Each mp As Person In e.NewItems
RemoveHandler mp.DidSomething, AddressOf OnPersonDidSomething
Next mp
End Select
End Sub
这是对的吗? Move, Replace, Reset
行动的内容是什么?
答案 0 :(得分:2)
这是正确的。
对于Replace
,您应该从旧项目中删除处理程序并将其添加到新项目中。
对于Reset
,您应该感到恐慌,因为您无法知道删除或添加了哪些项目。