Hello everyone.
What I try to do
Consume a DataTemplateSelector for a single item => not a ListView which would have make things very easy. I don't need a ListView because each template might use a ListView (and we know that a ListView inside a ListView isn't the best idea) and I only have one template to display. Not a list.
What I used to do
In the past, I would have used several element with a binding on the IsVisible property and use a converter to chose which element should be visible.
E.G (I simplified the content but imagine that instead of Label, there would have a custom ContentView) :
<StackLayout>
<Label IsVisible="{Binding Team, Converter={StaticResource TeamToVisibilityConverter}, ConverterParameter='1'" Text="Template1"/>
<Label IsVisible="{Binding Team, Converter={StaticResource TeamToVisibilityConverter}, ConverterParameter='2'" Text="Template2"/>
<Label IsVisible="{Binding Team, Converter={StaticResource TeamToVisibilityConverter}, ConverterParameter='3'" Text="Template3"/>
</StackLayout>
What I'd like to do
<ContentView BindingContext="{Binding Team}">
<ContentView.ControlTemplate>
<ControlTemplate DataTemplateSelector.ElementTemplateContent="{StaticResource TeamTemplateSelector}" />
</ContentView.ControlTemplate>
</ContentView>
I would like to make things smarter. I'm trying to figure out how to use a DataTemplateSelector for a single ContentView.
Do anyone have found something interesting : a smart way to accomplish what I'm looking for ?
The closest thing I found is https://forums.xamarin.com/discussion/84810/datatemplates-and-contentcontrols#latest
But I think and hope there is a better way to do that in 2019
PS : there isn't any topic on that on the documentation and I only found exemple with ListView.
Workaround found
Use the BindableLayout with a collection of one item. But IMHO, that's far from the best option and it requires to either have a converter that return a list from a single object or to add a collection of a single element in the view model.
Thanks everyone.