Your question is opinion based and so you may get wildly varying answers. Your comments say "No, don't do that because you'll break the MVVM pattern", but that depends on the size and longevity and professionalism of your project and your programming style in general. In fact, using the code behind for view related logic is perfectly fine in MVVM, although I'm sure some die hard MVVM developers will disagree with this statement.
However, if you'd prefer to not use the code behind, there are other ways to load views from the view model and you can find a common one explained in the WPF MVVM navigate views question. There's too much there to show here, but the general idea is this:
All of your view models should extend an abstract base class, let's call it BaseViewModel
:
public BaseViewModel ViewModel { get; set; }
In App.xaml, you could declare some simple DataTemplates
to connect the views with the view models:
<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
<Views:MainView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:PersonViewModel}">
<Views:PersonView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:CompanyViewModel}">
<Views:CompanyView />
</DataTemplate>
Now, wherever you use one of your BaseViewModel
instances in your application, these DataTemplate
s will tell the framework to display the related view instead. You can display them like this:
<ContentControl Content="{Binding ViewModel}" />
To switch to a new view, you just need to set the ViewModel
property from the MainViewModel class:
ViewModel = new PersonViewModel();
Please refer to the linked question for further information on this method.
Of course, once you have implemented this system, you'll probably be thinking 'How can I change the view model in the main view model from a child view model?' The answer to that can be found in the How to call functions in a main view model from other view models? question.