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.