Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 377
Blake Mumford Avatar asked Mar 04 '12 00:03

Blake Mumford


People also ask

What is DataTemplate?

A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.

What is control template?

The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.

What is difference between a ControlTemplate and a DataTemplate in WPF?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).

What is ItemTemplate WPF?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.


1 Answers

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?

like image 161
chopikadze Avatar answered Sep 24 '22 19:09

chopikadze