My MainView.xaml contains my SmartForm View:
<Grid Margin="10">
<views:SmartForm/>
</Grid>
the SmartForm view loads an ItemsControl
<Grid Margin="10">
<ItemsControl
ItemsSource="{Binding DataTypeViews}"/>
</Grid>
which is an ObservableCollection of DataTypeViews:
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);
}
and each view shows the label and textbox which builds a form:
<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>
How do I bubble the Textbox changes that happen in DataTypeViewModel up into SmartFormViewModel?
Or in other words: If ViewModel A contains a collection of ViewModel B, and a change happens in a ViewModel B, how can I bubble that change up to ViewModel A?
Although Kent is right above, not all changes in child view models have to do with properties, some might be a little bit more semantic than that. In such cases, implementing a variation of the Chain of Responsibility pattern might be a good fit.
In short
The "master" handler doesn't have to be a Singleton, its registry can depend on the parent view model itself.
Hope it's clear enough (sorry for not putting code)
You can just have the parent VM connect to the PropertyChanged event on the child VMs. It's kind of a PITA to keep track of the children who have been added/removed etcetera so you might instead consider storing your child VMs in my
ItemObservableCollection
:Then your parent VM can just listen for all changes to child items with:
I think you should employ the mediator pattern which you can read about here.
Basically it is a static class that allows ViewModels(or any class for that matter) to communicate with each other and pass arguments back and forth.
Basically ViewModel A starts to listening for a certain message type(e.g. ViewModelBChanged) and whenever that event happens ViewModelB just notifies anyone who's listening to for this message type, it can also pass any information it wants.
Here's the skeleton of a mediator.
ViewModel A would do this(probably in the constructor):
and then would have to declare a function like this:
and ViewModel B would call this whenever it want to tell ViewModel A
The mediator class would be in charge of invoking the callback function of viewModel A. And then everyone is happy.
Personally I like putting these string message values in a static class like this
So that you could do the following(instead of the above):
If this is unclear just google MVVM mediator and click to your hearts content :)
I solved this by just passing the ViewModel itself down into the ViewModels contained in it, here's a demo showing how it's done.
The non-WPF way is to create a static event on DataTypeViewModel. This allows you to fire the event from DataTypeViewModel when appropriate (in a property setter or property changed handler). Of course, you'll also have to register a listener to the event in SmartForm (requiring SmartForm to know about the DataTypeViewModel type).
Alternatively I think you could create your own custom routed event.