question

VigneshwaranS-4345 avatar image
0 Votes"
VigneshwaranS-4345 asked LeonLu-MSFT answered

How to get list size in Data template selector of Bindable layout in xamarin forms

I want to show different data template based on itemSource size of the bindable layout. I tried using DataTemplateSelector but I can't access the parent list size. How to get the size in the DataTemplateSelector.


<StackLayout
                                x:Name="dynamicStack"
                                BackgroundColor="White"
                                BindableLayout.ItemTemplateSelector="{StaticResource brandMultipleTemplate}"
                                BindableLayout.ItemsSource="{Binding CategoryBrand}"
                                HorizontalOptions="FillAndExpand"
                                Spacing="0"
                                VerticalOptions="FillAndExpand" />

<ContentPage.Resources>

        <ResourceDictionary>

            <DataTemplate x:Key="SingleBrandTemplate" />

            <DataTemplate x:Key="TwoBrandTemplate">

                <AbsoluteLayout
                    Margin="5"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">

                    <ffi:CachedImage
                        AbsoluteLayout.LayoutBounds="0.5,0.5"
                        AbsoluteLayout.LayoutFlags="PositionProportional"
                        Source="{Binding BrandLogo}" />

                </AbsoluteLayout>

            </DataTemplate>

            <selector:CategoryBrandTemplateSelector
                x:Key="brandMultipleTemplate"
                SingleBrand="{StaticResource SingleBrandTemplate}"
                TwoBrand="{StaticResource TwoBrandTemplate}" />

        </ResourceDictionary>

    </ContentPage.Resources>

class CategoryBrandTemplateSelector : DataTemplateSelector
    {
        public DataTemplate SingleBrand { get; set; }
        public DataTemplate TwoBrand { get; set; }
        public DataTemplate FourBrand { get; set; }
        public DataTemplate MultiPageBrand { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            var list = (List<CategoryBrand>)container.GetValue;

            if(list.Count == 4)
            {
                return FourBrand;
            } else if(list.Count == 2)
            {
                return TwoBrand;
            } else if(list.Count > 4)
            {
                return MultiPageBrand;
            } else
            {
                return SingleBrand;
            }
        }
    }


In the selector, the system throwing unable to cast to List exception.

dotnet-csharpdotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

You can use BindableLayout.GetItemsSource() to get the list size in Data template selector of Bindable layout,

protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            StackLayout sl = container as StackLayout;

            List<Person> ss=    BindableLayout.GetItemsSource(sl) as List<Person>;

            int countValue=ss.Count;
           
            return ((Person)item).Age >= 20 ? ValidTemplate : InvalidTemplate;
        }


Here is running screenshot.

82139-image.png



Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (38.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.