In MVVM are DataTemplates considered Views as UserControls are Views?
Asked Answered
F

3

7

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?

Forgetmenot answered 19/6, 2009 at 7:58 Comment(0)
M
7

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>
Malloy answered 19/6, 2009 at 10:2 Comment(6)
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.Forgetmenot
Basically, I put the "mapping" datatemplates in the App.xaml or the main window's XAML, and everything else is UserControls...Malloy
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.Forgetmenot
@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: #1026842Forgetmenot
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.Malloy
After a lot of reading, a lot of suffering, and some insight, this is the most simple and powerful way to implement pure-WPF (no framework) ViewModel-First MVVM.Henryhenryetta
R
1

What is making the most sense to me at this point in transitioning to MVVM is:

UserControls: Group views and controls together that are meant to execute an entire process for the user. I use a usercontrol for a slide-out drawer that I created that allows a user to flip through different classifications of parts (where each main part category routs the user through it's own selection process/wizard). Also look at Charting Controls.

Views: Basically a grid that groups related data and controls that populate desired data at the smallest level so it can be reused.

DataTemplates: Group facts about "items" like in a listbox or GridView. I think of this as just how the what text represents an object you're trying to gain information about in a list. So a control template would wrap the data template blocks to control the display behavior of that collection of data template-wrapped items.

That's how I understand it, but the boundaries are very fuzzy. I like the Apress book Pro Expression Blend 4 by Andrew Troelson. He doesn't really give any data-heavy examples, though.

Rascally answered 12/8, 2012 at 6:16 Comment(0)
B
0

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.

Bis answered 18/5, 2010 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.