With Windows Presentation Foundation (WPF) it is very easy to switch your datatemplate in dependency of for example the type of data you want to bind. But Silverlight does not support this “Out-of-box”. So we have investigated some time in our current project to get a similar mechanism. The idea for my solution is, that we have a area in our form which should contains dynamic objects.One of the conditions for the solution was that it should support the common MVVM pattern.
The key point of my solution is to use a DependencyProperty and bind this to our viewmodel instance.
Step 1: Write a user control for the dynamic area which binds the view model
The DetailsViewSelector have two bindings
- Content: Binds the view model to the choosen control
- TemplateType: Binds also the view model to select the view by naming comventions
In my sample I habe only one DataTemplate and a empty Data Template if no corresponding objects could be bound.
Step 2: Implement the Detail View Selector
Code for the Control:
To have a very flexible mechanism the DetailsViewSelector works with naming conventions to choice the View for the given view model.