views:

64

answers:

1

Silverlight 4 is out and it seems we've missed the DataTemplate DataType functionality in this release again, which is pretty pivotal for MVVM support IMHO. For my WPF apps, at this point, I'm pretty used to globally adding DataTemplates for my Views to my Application.Resources with DataTypes for my corresponding ViewModels:

ie.

<DataTemplate DataType="{x:Type viewModels:myViewModel}">
<views:myView/>
</DataTemplate>

I like this approach, since all my bound ViewModels automatically display the correct content...especially useful when I have some ItemSource in my view bound to a collection of ViewModels... This, for example, will automatically make sure each tab in a TabControl bound to a Collection<SomeViewModel> displays the view associated with SomeViewModel.

Some things I tried for SL 3 include:

  • Creating a "DataTemplatePresenterContentControl" which automatically applies a DataTemplate for the Content when the control has loaded

  • Using a TypeConverter, applied dynamically on control load, walking down the visual tree looking for data bound objects

  • Using a style, applied dynamically on control load, walking down the visual tree looking for data bound objects

However, none of these approaches really address the situation I mentioned above in an acceptable way, which is really key.

So, since this still isn't possible out of the box in Silverlight 4, I'd appreciate to know if anyone has yet come up with some reasonable alternatives.

Thanks.

A: 
Brian Genisio
I'm using Prism's RegionManager too, but could you elaborate a bit more please on the specifics of how you're doing this?
JeffN825
Elaboration is in my edit above.
Brian Genisio
Yes, thanks. I like the approach. But it still does not address the one issue I mentioned above; binding to an IEnumerable<T> - for example binding a TabControl to a Collection<MyViewModelClass> and expecting each tab to display the MyViewForMyViewModelClass UserControl. Or is there a way to adapt your approach to support that? Thanks.
JeffN825
Right.. in the template for the TabControl, include the ContentControl with a region name that is type-specific.Then, just register different views to show for the different type-specific region names.
Brian Genisio
Ok, cool. I'm liking this. Then how/where to I call to register the view with the region (ie. RegionManager.RegisterViewWithRegion)?
JeffN825