Initializing child windows in MvvM

I have a MvvM application that is working very fine. However I am not sure if I'm writing a clean code, especially the part handling child windows with their controller.

So to begin, i have this structure that helps me delegate methods to the controllers to open child windows when needed :

public struct ShowActions
{

public ShowActions(Action show, Action showDialog)
{
this.Show = show;
this.ShowDialog = showDialog;
}
}


In the MainController class, I have a WindowServiceProvider property that is just a dictionary linking a type of Controller to a ShowActions structure :

public Dictionary<Type, ShowActions> WindowServiceProvider { get; set; }


The main window intializes the child windows like this :

public MainWindow()
{
InitializeComponent();
var mainController = new MainController();
this.DataContext = mainController;

var authenticationController = new AuthenticationController(mainController);
var modelController = new ModelController(mainController);

AuthenticationWindow authenticationWindow = null;
NewModelWindow modelWindow = null;

{
authenticationWindow = new AuthenticationWindow(authenticationController)
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = this
};
authenticationWindow.ShowDialog();
}));

{
modelWindow = new NewModelWindow(modelController)
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = this
};
modelWindow.ShowDialog();
}));
}


Is this a good way to code ? I fill like it may be a bit heavy for what it's doing. And also, is it bad to have many controllers when one can handle everything ?

• I have a doubt: why do you talk about controllers in a MVVM application? I'd say that in your code they're the VM part (at least because you assign a controller to the DataContext) but...I feel weird about it. Also: why this code is in the View part instead of the VM part? – Adriano Repetti Mar 13 '18 at 11:23
• Bad habit, I call the ViewModel Controller but there are just ViewModels. Since the ViewModels are not allowed to manipulate the properties of the view I do the initialization of the child views and their viewModels in the View. But I also do feel like there is something wrong with this way of doing. : / – Mohammed Larabi Mar 13 '18 at 13:26
• View models are (and should!) able change the properties in the view. For example: AuthenticationController.Password should be simply binded with the AuthenticationWindow.PasswordBox.Password thing. Also you should not expose services (which are just actions...) built in the view but they should be called from the relevant methods in the view model (methods which are...commands). Binding with commands is in-built, if you have actions in a dictionary then you need to write more and more code in your views to call them and, well, that's not how MVVM works. – Adriano Repetti Mar 13 '18 at 14:11
• Using WPF like WinForms (or mixing with a MVC paradigm) is possible but your code will finally be MUCH more complicate than it should/may be. In short: in this small snippet there are things to review but the drawbacks of this approach are more clear when you include surrounding code. You find yourself writing more and more code to bind things together (and you get all the disadvantages of MVVM without any of its huge advantages) – Adriano Repetti Mar 13 '18 at 14:12
• @AdrianoRepetti How can I bind Action between them ? Since Password() is a method for security reasons. But I'm starting to understand that I'm mixing MVC concepts with MvvM and I'll probably just reread the documentation, thanks for the lead. Then how can I make a ViewModel call for a child window ? – Mohammed Larabi Mar 13 '18 at 14:58