Current I have some Views linked to ViewModels using code similar to the following:
<Application.Resources>
<DataTemplate DataType="{ x:Type vm:AgeIndicatorViewModel}">
<v:AgeIndicatorView />
</DataTemplate>
</Application.Resources>
I have two questions regarding this:
-
Does this method allow me to only link one View to each View Model (I think it does improse this limitation on me, but want to be sure)
-
When using this method, where should I put all of my DataTemplate declarations? At the moment there are only a few, and they are all in App.Xaml – Is there a better location for these, or is App.Xaml fine / Best location?
The most important question is the second really, as at the moment I want to link my ViewModel to my View in this way, as it requires no external libraries etc.
The way my ViewModels are setup, with their Properties and Commands etc is all working already.
Yes. If you’re trying to link multiple ViewModels to multiple Views, you need to encapsulate them within a separate VM, and add a new DataTemplate.
App.Xaml is fine, or really any place in the visual hierarchy above where the DataTemplate will be used.
That being said, if the project gets to be a very large scale project, it’s often nicer to start using Merged Resource Dictionaries – this allows you to setup resource dictionaries “near” where you define the View/ViewModel pairs, but then use them at a higher level (ie: merge them into App.Xaml).