如何在类之间传递PropertyChanged事件?

时间:2015-05-30 00:02:13

标签: .net wpf vb.net

我一直在WPF中使用数据绑定,我一直在努力应对以下场景:

如果我有一个类是另一个类的朋友/公共属性,那么当属性类的属性发生变化时,是否可以通知包含类?

如果我重新分配属性类,我可以让通告符在属性类中工作,或者在包含属性中工作,但是我无法通过第三种方式使事件冒泡起作用。

例如,以下触发事件是我可以收到通知的事件:

myParentClass.child = myChild

child.childProperty = "A property"

但我希望在myParentClass时收到通知:

myParentClass.child.childProperty = "A property"

从以下模板示例代码:

Public Class cChildClass
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _childProperty As String
    Public Property childProperty As String
        Set(ByVal value As String)
            If Not _childProperty = value Then
                _childProperty = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("childProperty"))
            End If
        End Set
        Get
            Return _childProperty
        End Get
    End Property

End Class

Public Class cParentClass
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _child As cChildClass
    Public Property child As cChildClass
        Set(ByVal value As cChildClass)
            _child = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("child"))
        End Set
        Get
            Return _child
        End Get
    End Property
End Class

2 个答案:

答案 0 :(得分:0)

Private _child As cChildClass
    Public Property child As cChildClass
        Set(ByVal value As cChildClass)
            _child = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("child"))

            _child.PropertyChanged += Function(sender, e) 
                If e.PropertyName = "childProperty" Then
                '....WHEN EVENT CHANGED, YOU DO ANYTHING HERE
                End If    
            End Function
        End Set
        Get
            Return _child
        End Get
    End Property        

答案 1 :(得分:0)

完成答案,根据Plutonix的建议:

cParentClass中,Private _child As cChildClass已更改为Private WithEvents _child As cChildClass

然后我可以在以下函数中调用私有变量:

 Private Sub child_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles _child.PropertyChanged
        Console.WriteLine(e.PropertyName & " has changed!")
 End Sub

完整的示例带有更正,如下所示:

Public Class cChildClass
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _childProperty As String
    Public Property childProperty As String
        Set(ByVal value As String)
            If Not _childProperty = value Then
                _childProperty = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("childProperty"))
            End If
        End Set
        Get
            Return _childProperty
        End Get
    End Property

End Class

Public Class cParentClass
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private WithEvents _child As cChildClass
    Public Property child As cChildClass
        Set(ByVal value As cChildClass)
            _child = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("child"))
        End Set
        Get
            Return _child
        End Get
    End Property

    ....

    Private Sub child_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles _child.PropertyChanged
        Console.WriteLine(e.PropertyName & " has changed!")
    End Sub

End Class
相关问题