【发布时间】:2019-02-15 09:18:08
【问题描述】:
我直接从一张图片开始,展示我拥有的结构,这样我就可以用图片来提问。
我有一个这样的ParentModel:
Public Class ParentModel
public Property ModelValue_A As String
Public Property ModelValue_B As String
End Class
我有一个ParentViewModel,它有两个ChildViewModel 类型的属性。
Public Class ParentViewModel
Public Property Parent As ParentModel
Public Property ChildViewModel_A As ChildViewModel
Public Property ChildViewModel_B As ChildViewModel
Sub New
ChildViewModel_A = New ChildViewModel()
ChildViewModel_B = New ChildViewModel()
End Sub
End Class
我的ParentView是这样的:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding ChildViewModel_A}"/>
<ContentPresenter Content="{Binding ChildViewModel_B}"/>
</StackPanel>
</DataTemplate>
我的ChildViewModel是这样的:
Public Class ChildViewModel
Private _ChildValue As String
Public Property ChildValue As String
Get
Return _ChildValue
End Get
Set
_ChildValue = Value
NotifyPropertyChanged(NameOf(ChildValue))
End Set
End Class
我的ChildView是这样的:
<DataTemplate>
<TextBox Text="{Binding ChildValue}" />
</DataTemplate>
我的NotifyPropertyChanged 方法:
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub NotifyPropertyChanged(info As [String])
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
当我启动应用程序时,我得到一个类似于上图的视图。在那里,我可以通过输入ChildView 的TextBox 来更改ChildValue。但是,我仍然没有每个ChildValue 与其对应的ParentViewModel 属性之间的连接/关系:ChildViewModel_A 和ChildViewModel_B。
我的问题是:如何通过更改ChildValue 的ChildViewModel_A 来更改ModelValue_A,并分别通过更改ChildViewModel_B 的ChildValue 来更改ModelValue_B? p>
【问题讨论】:
-
ParentViewModel 可以订阅 2 个 NotifyPropChanged 事件。您必须在某处为拆分 PropA / PropB 编码。
-
@HenkHolterman “订阅 2 个 NotifyPropChanged 事件”是什么意思?