I need help with a change of perspective.
I got stuck trying to approach UWP in a way I used to do in WPF regarding a MVVM pattern for managing UserControls dynamically.
I naturally tried to perform the same pattern in UWP but got stuck on various things like UWP not supporting 'x:Type' ...
Situation is; time to rethink this approach and look for a new direction. Seems I'm forced to abandon to use implicit binding in a similar fashion to the WPF pattern, using the Content property of a ContentPresenter and a VM property 'of type Object', which maintain a selected ViewModel. It was a simple and clean approach for matching up the correct View automagically with the VM set in ActiveViewModel.
the below was such a simple way of managing many views all over the place, odd MS not fixing this? But, back to the big Q: what now in UWP!?
<ContentPresenter Content="{Binding ActiveViewModel}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type local:OneViewModel}">
<local:OneView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:TwoViewModel}">
<local:TwoView />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
What Shall I do instead of this!? Anyone found a new efficient way of doing it? I got stuck in my stubborn mind and need someone to kick my butt so I go forward. Getting to old to change, but due to this profession it seems I constantly have to. :)
Looking at the DataTemplate documentation, there's a paragraph explaining the situation which you are trying to figure out.
Here, you have an example on how you can select distinct
DataTemplatefor items in a control such as aListViewbased on defined conditions.Your situation is a bit different from the one described above, but the solution should be within what is explained above.
DataTemplateSelector, and override theSelectTemplateCoremethods exposed by it, where you define the logic of whatDataTemplateshould be selected for the specific presented object.DataTemplate, which identify each singleDataTemplatetemplate object, you pretend to be able to choose from.DataTemplateresources on an higher level object, such as the Page itself.DataTemplateSelectorDerived class in XAML as a resource and set each of the properties exposed above of type DataTemplate to the analogousDataTemplatestatic resource.ContentTemplateSelectordependency property, by setting it your custom DataTemplateSelector.With this logic, it should be possible to have your
ContentPresenterdecide correctly between whichDataTemplateit should choose from, based on your required UI logic.