Question

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?

Was it helpful?

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>

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top