views:

109

answers:

1

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

A: 

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.

Igor Zevaka
Ok yeah - but I want to be able to create Views dynamically - like in the article I posted above. I'll have a number of views and they'll be added into a TabControl - so how will I be able to add to my collection of 'Workspaces' and have it render the view correctly ?? BTW I'm using the MVVM Light toolkit...
IrishJoker
Don't think you could that out of the box without requiring something like Prism - http://compositewpf.codeplex.com/ It's worth exploring anyway.
Igor Zevaka
Silverlight 4 has implicit styling - would it work ??
IrishJoker
I haven't tried templating my viewmodels like that, it might.
Igor Zevaka