Blake Mumford
Blake Mumford

Reputation: 17721

Where should I define my datatemplates?

I'm trying to work out the best way to couple my Views and ViewModels in MVVM and I've settled on the ViewModel-first approach using typed DataTemplates, as described in this article and in this answer. I'm using Prism and have multiple modules which have their own projects/class libraries.

My question is: Where should my DataTemplates live in my solution?

  1. Should I put the DataTemplates in a Resource Dictionary which lives in the same project that has the types/ViewModels it renders?
  2. Should I put the DataTemplates in a Resource Dictionary which lives in the project which has the application's MainWindow (i.e. Shell.xaml)?
  3. Should these Resource Dictionaries then be added to App.Current.MainWindow.Resources.MergedDictionaries?

I hope this is enough information to describe what I'm trying to do.

Update: see comments of selected answer.

Upvotes: 12

Views: 7278

Answers (3)

Herman Van Der Blom
Herman Van Der Blom

Reputation: 812

According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory. A Universal app has this directory for both the Windows UI as for the Windows Phone UI so its not in the Shared project because they are not the Same. Don't use the Converge PRISM architecture. Its completely wrong designed! That was not written with a Windows and a Windows Phone architecture in mind but like they call it Converged. It should have been completely redesigned like it works in Microsofts App Studio. Don't look for Dependency Injection its not in it and not needed. Most use Dependency Injection for stub or fake interfaces. The DataContext for design data works now so good with json data that a Dependency Injection component would be overkill.

Upvotes: 0

chopikadze
chopikadze

Reputation: 4239

I'm sure that the best way here is to use Themes\Generic.xaml resources file. This is file (it should be exactly in folder Themes and has name exactly Generic.xaml) used by WPF/Silverlight themes engine and contains resources shared through whole application. You can also create separate file in folder Themes with name like Generic.DataTemplates.xaml and add link to it from Generic.xaml. Google knows a lot about generic.xaml or you can see more details in my answer here: WPF Prism - Where to put Resources?

Upvotes: 2

King Chan
King Chan

Reputation: 4302

Updated to explain more clear.

I will say if your DataTemplate is generic: i.e You have a UserControl that binds to a ViewModel, and that ViewModel has BaseViewModel, which expose some sort of properties. Your DataTemplate is displaying those properties. So you can use this DataTemplate on every ViewModel that Implement the BaseViewModel.

Is better to put it in App.xaml, so you will able to pull it out with the Key and apply on different place in your project.

But if your DataTemplate is very specific,

i.e There is a UserControl that only binds on the specified property in that ViewModel and you know no other control will binds to that ViewModel, you will want to put into the same Xaml file's Resources or where you define your UserControl.

Upvotes: 2

Related Questions