我的MainView.xaml包含了我的SmartForm视图:
<Grid Margin="10">
<views:SmartForm/>
</Grid>
SmartForm视图加载一个ItemsControl
<Grid Margin="10">
<ItemsControl
ItemsSource="{Binding DataTypeViews}"/>
</Grid>
这是一个DataTypeViews的ObservableCollection:
List<FormField> formFields = new List<FormField>();
formFields.Add(new FormField { IdCode = "firstName", Label = "First Name", Value = "Jim" });
formFields.Add(new FormField { IdCode = "lastName", Label = "Last Name", Value = "Smith" });
formFields.Add(new FormField { IdCode = "address1", Label = "Address 1", Value = "123 North Ashton Rd." });
formFields.Add(new FormField { IdCode = "address2", Label = "Address 2", Value = "Box 23434" });
formFields.Add(new FormField { IdCode = "city", Label = "City", Value = "New Haven" });
formFields.Add(new FormField { IdCode = "state", Label = "State", Value = "NM" });
formFields.Add(new FormField { IdCode = "zip", Label = "Zip Code", Value = "34234" });
foreach (FormField formField in formFields)
{
DataTypeView dtv = new DataTypeView();
DataTypeViewModel dtvm = new DataTypeViewModel(formField);
dtv.DataContext = dtvm;
DataTypeViews.Add(dtv);
}
每个视图显示标签和文本框,构建一个表单:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="400"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0">
<TextBlock Text="{Binding Label}" FontSize="14"/>
<TextBlock Text=": " FontSize="14"/>
</StackPanel>
<TextBox Grid.Column="1" Text="{Binding Value}" FontSize="12"/>
</Grid>
我该如何将DataTypeViewModel中的文本框更改冒泡到SmartFormViewModel中?
换句话说:如果ViewModel A包含一个ViewModel B的集合,并且ViewModel B发生更改,我该如何将该更改冒泡到ViewModel A?