简体   繁体   English

响应后代类PropertyChanged事件

[英]Respond to Descendent Class PropertyChanged Event

I have a class, BaseSample, that uses another class, MainData, as one of its fields: 我有一个类BaseSample,它使用另一个类MainData作为其字段之一:

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. MainData依次具有一个类ProcessData作为字段的集合,而ProcessData则具有一个类Measurement的集合作为字段。 Ie: 即:

MainData 主数据

 |- IEnumerable (Of ProcessData) |- IEnumerable (Of Measurement) 

These classes are entities in a LINQ to SQL file. 这些类是LINQ to SQL文件中的实体。 In the Measurement class, I raise the PropertyChanged event when the CrucibleOxidizedMass property changes: 在Measurement类中,当CrucibleOxidizedMass属性更改时,我引发PropertyChanged事件:

Private Sub OnCrucibleOxidizedMassChanged()

    RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("CrucibleOxidizedMass"))

End Sub

In the BaseSample class, I want to respond to the CrucibleOxidizedMass PropertyChanged event in any of the descendent Measurment instances. 在BaseSample类中,我想在任何后代Measurment实例中响应CrucibleOxidizedMass PropertyChanged事件。 How would I do this? 我该怎么做?

Iterate through all ProcessDates and for each one iterate through each Measurement and subscribe to the event. 遍历所有ProcessDate,并为每个遍历每个Measurement并订阅事件。

In C# code: 在C#代码中:

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 . 另外,你应该从个体事件时,无论是测量从删除退订ProcessData或当ProcessData从取出SampleData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM