Consuming a DataTemplateSelector in a single ContentView : No ListView

NicolasKrierNicolasKrier FRMember ✭✭✭
edited March 4 in Xamarin.Forms

Hello everyone.

What I try to do
Consume a DataTemplateSelector for a single item => not a ListView which would have made 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 :D

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.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Are you sharing a successful experience or asking a better approach?

  • NicolasKrierNicolasKrier FRMember ✭✭✭
    edited March 5

    Hey @LandLu, I'm looking for a working approach without using a ListView or a BindableLayout (which is a workaround, not the best way to go IMHO).

    The what I'd like to do isn't working. That's just some code written to show the result I'd like to have

  • RHudsonRHudson CAMember ✭✭✭
    edited March 5

    That XAML you wrote is a dog's breakfast.

    In some situations, I've found it easier to build the page in code rather than XAML.

    Then you have total control. You can use the C# switch() construct, if/else, etc. to insert into the ContentView whatever you want at runtime.

  • RHudsonRHudson CAMember ✭✭✭

    @NicolasKrier Furthermore...

    You might want to look at the new bindable layout https://blog.xamarin.com/xamarin-forms-3-5-a-little-bindable-love/

    A new feature XF 3.5, this might work for you

  • NicolasKrierNicolasKrier FRMember ✭✭✭
    edited March 7

    Hello @RHudson thanks for your answer. I know it can be achieved in code behind but I'm looking for a Xaml way (because I love to learn new things).

    For the Bindable layout, may I suggest you to read any of my previous post in this thread where I mention that it works with the bindable layout but it requires an enumeration of object whereas I only got a single one. I invite you to read my answers to get the why I would like to avoid it.

    Here is the part you might want to read :

  • NicolasKrierNicolasKrier FRMember ✭✭✭
    edited March 19

    Hello, sorry I was in vacations :)

    I thought about creating my own ContentControl too but was hopping that a native way would be available now.

    I hope it will be inside the box one day (I really believe it's possible since they released the BindableLayout :smile: )

    Thank you @MuhammadAbuMandour . I will implement it this week and will update the post to show the final control and mark your message as the answer.

Sign In or Register to comment.