views:

1135

answers:

2

In MVVM, every View has a ViewModel. A View I understand to be a Window, Page or UserControl to which you can attach a ViewModel from which the view gets its data.

But a DataTemplate can also render a ViewModel's data.

So I understand a DataTemplate to be another "View", but there seem to be differences, e.g. Windows, Pages, and UserControls can define their own .dlls, one type is bound with DataContect the other through attaching a template so that Windows, Pages, UserControls can can be attached to ViewModels dynamically by a ServiceLocator/Container, etc.

How else are DataTemplates different than Windows/Pages/UserControls when it comes to rendering a ViewModel's data on the UI? And are there other types of "Views" other than these four?

+3  A: 

The way I use it, the DataTemplate is actually the way to link the View to the ViewModel. Typically my DataTemplates in MVVM look like that :

<DataTemplate DataType="{x:Type vm:FooViewModel}">
    <v:FooView />
</DataTemplate>
Thomas Levesque
interesting, but then how does the rests of your XAML look? I tried a small example of this, compiled it and Visual Studio said it needed Administrator permissions (Vista), odd. Would like to see how you use this pattern in a project.
Edward Tanguay
Basically, I put the "mapping" datatemplates in the App.xaml or the main window's XAML, and everything else is UserControls...
Thomas Levesque
Are you using a ContentControl to pull in the ViewModel then, I'm trying this <ContentControl Content="{Binding CurrentPageViewModel}"/> with a data template like yours that does this "<DataTemplate DataType="{x:Type vm:PageModelsViewModel}"><v:PageModelsView /></DataTemplate> but it doesn't link them up, it just displays nothing.
Edward Tanguay
@Thomas, this was really helpful, it helped me build my MVVM menu pattern I was after which however led to another issue here, perhaps you have solved this one as well: http://stackoverflow.com/questions/1026342/how-can-i-tell-my-datatemplate-to-bind-to-a-property-in-the-parent-viewmodel
Edward Tanguay
Yes, I usually use a ContentControl to display the ViewModel. For the DataTemplate to be used, it must be accessible from the ContentControl XAML context.
Thomas Levesque
A: 

I would not recommend using a DataTemplate to bind the view to the viewmodel. I am currently having performance issues using this approach as the view is refreshed everytime the view gets focus. As you can see here and here

Unfortunatley I have not found a better option as of yet.

Eli Perpinyal