Is there some obvious method for dealing with DataTemplates of which I am unaware?
For instance:
I have a ContentControl which will be used to display the various application views. As far as I am aware, for each control there may exist one DataTemplate per DataType. (i.e. I could add a DataTemplate to the ResourceDictionary to display a DetailsView when the ContentControl has a PersonViewModel set as its DataContext or a ListView when the ContentControl has a PersonManagerViewModel set as its DataContext). But how might I go about choosing between a DetailsView and an EditView when the ContentControl’s DataContext is set to a PersonViewModel?
A few options:
When defining ‘region’ View Models, always use a 1:1 map between a ViewModel and a View. This is the most typical approach. If you’re thinking of using the same ViewModel for multiple Views, it’s likely that the ViewModel is doing too much. The shared logic probably belongs in its own Model class that two or more different ViewModel classes would use.
Define a custom DataTemplateSelector to disambiguate between possible matches. This is a bit uglier, as you no longer have the nice magic ‘find the default’ behavior.
Only allow one of the templates to be the ‘default’. Other templates must be referenced by Key by the specific Views that need them.