0
votes

I was reading this article - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

And I came across this piece of code in the WPF Demo application that came with the article.

This template applies a CustomerView to an instance of the CustomerViewModel class shown in the main window.

<DataTemplate DataType="{x:Type vm:CustomerViewModel}">  
    <vw:CustomerView />  
</DataTemplate>

I substituted the angle brackets for square brackets - not sure how to post them.

The code is in the MainWindowResourses.xaml and the code starts on line 19.

Anyone know how I can do this in Silverlight ?? We don't have the DataType and I need to be able to tell the app that this View is associated with this ViewModel - so I can create a tab control with different view like the demo app.

Cheers,

EC

1
I just edited your code block for you - can you check it is what you intended? Cheers. - slugster
Yeah that's the business :) I think I found out the problem though. I'm using the MVVM Light toolkit - but I'm not using the Locator part of the application - I think that the locator does what I need - need to read up on it :) Thanks - Eoinii
I've tagged the question mvvm-light. Laurent Bugnion, the man behind MVVM Light Toolkit, who frequently trawls through here, might have a better insight in the matter. - Igor Zevaka

1 Answers

0
votes

This is an example of WPF implicit styling, where a style is applied to all the controls in the project. This is not supported in Silverlight.

To get around this you need to instead place your view controls in the markup and set their DataContext to the viewmodel.

<Window.Resources>
    <vm:CustomerViewModel x:Key="theViewModel">  
<Window.Resources>

<vw:CustomerView DataContext={StaticResource theViewModel}/>  

theViewModel doesn't have to come from the resources section, it could be a property in the hosting XAML control/page.