NotifyPropertyChanged不起作用,为什么?

时间:2016-04-13 17:54:54

标签: inotifypropertychanged

我正在尝试使用从ClassB到ClassA的INotifyPropertyChanged调用属性,但这不起作用!我做错了什么?

A类

Public Class ClassA
Implements System.ComponentModel.INotifyPropertyChanged

Public Event PropertyChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.PropertyChangedEventArgs) Implements
System.ComponentModel.INotifyPropertyChanged.Prope rtyChanged

Protected Overridable Sub OnPropertyChanged(ByVal e As
System.ComponentModel.PropertyChangedEventArgs)
RaiseEvent PropertyChanged(Me, e)
End Sub

Private m_prop1 As String

Public Property Prop1() As String
Get
Return m_prop1 
End Get
Set(ByVal value As String)
m_prop1 = value
End Set
End Property

End Class

B类

Public Class Class  B
Inherits Class A

Private m_prop2 As String

Public Property Prop2() As String
Get
Return m_prop2 
End Get
Set(ByVal value As String)
m_prop2 = value
Dim e As New System.ComponentModel.PropertyChangedEventArgs("Prop1")
OnPropertyChanged(e)
End Set
End Property

End Class

此示例有效!!

A类

Public Class ClassA
Implements System.ComponentModel.INotifyPropertyChanged

Public Event PropertyChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.PropertyChangedEventArgs) Implements
System.ComponentModel.INotifyPropertyChanged.Prope rtyChanged

Protected Overridable Sub OnPropertyChanged(ByVal e As
System.ComponentModel.PropertyChangedEventArgs)
RaiseEvent PropertyChanged(Me, e)
End Sub

Private m_prop1 As String

Public Property Prop1() As String
Get
Return m_prop1 
End Get
Set(ByVal value As String)
m_prop1 = value
End Set
End Property

End Class

B类

Public Class Class  B
Inherits Class A

Private m_prop2 As String

Public Property Prop2() As String
Get
Return m_prop2 
End Get
Set(ByVal value As String)
m_prop2 = value
Dim e As New System.ComponentModel.PropertyChangedEventArgs("Prop3")
OnPropertyChanged(e)
End Set
End Property

Private m_prop3 As String

Public Property Prop3() As String
Get
Return m_prop3 
End Get
Set(ByVal value As String)
m_prop3 = value
End Set
End Property

End Class

但我想取代PropertyChanged" Prop3"呼叫" Prop1"从另一个班级。这可能吗?

1 个答案:

答案 0 :(得分:0)

我解决了,我在ClassB中创建了一个变量m_ClassA,然后我把它作为OnPropertyChanged方法的参数“sender”。

A级
Public Class ClassA
Implements System.ComponentModel.INotifyPropertyChanged

Public Event PropertyChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.PropertyChangedEventArgs) Implements
System.ComponentModel.INotifyPropertyChanged.Prope rtyChanged

Protected Overridable Sub OnPropertyChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.PropertyChangedEventArgs)
RaiseEvent PropertyChanged(sender, e)
End Sub

Private m_prop1 As String

Public Property Prop1() As String
Get
Return m_prop1 
End Get
Set(ByVal value As String)
m_prop1 = value
End Set
End Property

End Class
B级
Public Class ClassB
Inherits ClassA

Private m_ClassA as ClassA

Public Sub New(ByVal c as ClassA)
   m_ClassA = c
End Sub

Private m_prop2 As String

Public Property Prop2() As String
Get
Return m_prop2 
End Get
Set(ByVal value As String)
m_prop2 = value
Dim e As New System.ComponentModel.PropertyChangedEventArgs("Prop1")
OnPropertyChanged(m_ClassA,e)
End Set
End Property

End Class