我可以像这样从一对一的关系中冒泡一个事件
Public Class Husband
Public WithEvents Wife As Wife
Public Sub WifeChangedLastName() Handles Wife.LastNameChanged
MsgBox("Wife Changed Last Name")
End Sub
End Class
Public Class Wife
Public _LastName As String
Public Property LastName As String
Get
Return Me._LastName
End Get
Set(ByVal Value As String)
Me._LastName = Value
Raise Event LastNameChanged(Me, EventArgs.Empty)
End Set
End Property
Public Event LastNameChanged As EventHandler
End Class
但是我如何对一对多的关系做类似的事情呢?这是我到目前为止所拥有的:
Public Class Organization
Public WithEvents Group As New Group 'A one-to-one relationship
Public Sub PersonAddedToGroup() Handles Group.PersonAdded
MsgBox("A person has been added to the group.") 'This works
End Sub
'I want to do something here when a person's name changes
End Class
Public Class Group
Public WithEvents People As List(Of Person) 'A one-to-many relationship
Public Sub Add(ByVal Person As Person)
Me.People.Add(Person)
RaiseEvent PersonAdded(Me, EventArgs.Empty)
End Sub
Public Event PersonAdded As EventHandler
End Class
Public Class Person
Private _Name As String
Public Property Name As String
Get
Return Me._Name
End Get
Set(ByVal Value As String)
Me._Name = Value
RaiseEvent PersonChanged(Me, EventArgs.Empty)
End Set
End Property
Public Event PersonChanged As EventHandler
End Class
我想在组织中处理 PersonChanged 事件。我该怎么做呢?