views:

40

answers:

3

Doing something like this:

<DataTemplate DataType="{x:Type vm:AllCustomersViewModel}">
<vw:AllCustomersView />
</DataTemplate>

works in a ResourceDictionary for when I want to apply a ViewModel to a UserControl as root, but how do I the same thing when I have a UserControl inside of a Page? Would I create a ResourceDictionary for all my Pages then at the top of each Page do something like:

<Page.Resources>
      <ResourceDictionary Source="../MainWindowResources.xaml"/>           
</Page.Resources>

Any help is greatly appreciated, thanks!

A: 

(I apologize for not commenting on the question. My comment is too long.)

Could you clarify the question, please ?

You have a UserControl that has the mentioned DataTemplate in its own resources, right ?

Now you want to use this control in a page, right ?

And your question is: do you need to declare the resources for the UserControl in each page that uses it ?

If only the User Control itself needs access to these resources, no, there is no need to redeclare them elsewhere. If you want these resources to be available for the hosting page (or some element up the hierarchy), the resources should be declared there. If they should be available anywhere in the app, it is recommended to declare them at the Application level.

Timores
Yes, I have a UserControl that I want inside of a Page. What I am asking is how do I apply the ViewModel from somewhere like the App.xaml to the UserControl inside of the page? Thanks!
Mike
In an MVVM app, you need some helper methods to make the connection between the view and the view model. I use the library from Mark Smith at: http://www.julmar.com/blog/mark/2009/08/04/MVVMHelpersV103.aspx. It defines a service that creates a new window and sets the Data Context correctly to the view model.
Timores
A: 

A better option would be to include the resources in your App.xaml. That way, anywhere that you are displaying your ViewModel in the application will get the template.

Abe Heidebrecht
A: 

I agree with Abe. All you have to do is add a Resource Dictionary file to the project (e.g. Resources.xaml

put

<DataTemplate DataType="{x:Type vm:AllCustomersViewModel}">
<vw:AllCustomersView />
</DataTemplate>

inside of it. And then in your App.xaml put the following:

<Application.Resources>
    <ResourceDictionary Source="Resources.xaml"/>
</Application.Resources>

So whenever WPF is trying to figure out how to render a class (that it currently doesn't know how to render) it looks in the controls resources, if there is no template there it checks the controls container resources. If not there, it keeps going up until there is no container. Then it goes to Application.Resources. If it finds it then it renders using that template. If not then it just calls ToString(). Put all your global stuff in Application.Resources, so you're not repeating yourself all over your app, Keep it DRY(Don't Repeat Yourself).

On a side note. I found this post to be the easiest solution to binding lots of ViewModels to Views without explicitly writing each one in a resource dictionary. It uses a IValueConverter to dynamically return the desired View.

Jose
But the problem is that AllCustomersView is <Page>, and you can't apply a dataTemplate this way: <DataTemplate DataType="{x:Type vm:AllCustomersViewModel}"><vw:AllCustomersView /></DataTemplate>to a Page, the DataTemplate is applied to things derivied from ContentControl such as a UserControl which AllCustomersView is not. I want to apply the DataTemplate to the UserControl inside of the AllCustomersView page.
Mike