I have a class, BaseSample, that uses another class, MainData, as one of its fields:
Protected _sampleData As MainData
Public Property SampleData() As MainData
Get
Return _sampleData
End Get
Set(ByVal value As MainData)
_sampleData = value
End Set
End Property
MainData in turn has a collection of a class, ProcessData, as a field and ProcessData has a collection of a class, Measurement, as a field. I.e.:
MainData
|- IEnumerable (Of ProcessData) |- IEnumerable (Of Measurement)
These classes are entities in a LINQ to SQL file. In the Measurement class, I raise the PropertyChanged event when the CrucibleOxidizedMass property changes:
Private Sub OnCrucibleOxidizedMassChanged()
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("CrucibleOxidizedMass"))
End Sub
In the BaseSample class, I w开发者_JAVA百科ant to respond to the CrucibleOxidizedMass PropertyChanged event in any of the descendent Measurment instances. How would I do this?
Iterate through all ProcessDates and for each one iterate through each Measurement and subscribe to the event.
In C# code:
foreach(var processData in value.ProcessDatas)
{
foreach(var measurement in processData.Measurements)
{
measurement.PropertyChanged += OnMeasurementPropertyChanged;
}
}
Be sure to unsubscribe from those events, when new sample data is set. Also, you probably should unsubscribe from individual events when either a measurement is removed from a ProcessData
or when a ProcessData
is removed from the SampleData
.
精彩评论