Search code examples
.netwpfmvvmviewmodeldatatemplate

In MVVM are DataTemplates considered Views as UserControls are Views?


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?


Solution

  • 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>