0

What is the best way to submit a window with multiple usercontrols, each with their own viewmodel?

I have a Form, with multiple child controls within it. The form itself has a Viewmodel for its direct controls, and each of the usercontrols have their own viewmodel.

The number of usercontrols differ depending on user selection so will not be the same each time.

The form is submitted in one go with a save button.

Is it a case of upon save click, that I should loop all the usercontrols and save their view models individually via reading their datacontext? Or is there a better way.

Thanks

1 Answers1

0
  1. Create a CompositeCommand on the ParentViewModel and pass it to all the dynamically created childviewmodels.

    public readonly CompositeCommand SaveAllCommand;

//Somewhere inside parent viewmodel

childViewModel = new ChildViewModel(this.SaveAllCommand);
  1. Then inside each of the ChildViewModels, register then own save command with this SaveAllCommand

    public ChildViewModel(CompositeCommand saveAllCommand)    
    {    
    saveAllCommand.RegisterCommand(new SaveChildCommand());
    }
    
  2. Finally when you execute the SaveAllCommand in the Parent, all the childViewModel's SaveCommand that are registered with the Command will get executed as well.

Saraf Talukder
  • 376
  • 2
  • 12