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

Vigneshwaran S 1 Reputation point
2021-03-27T11:24:26.163+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,741 Reputation points Microsoft Vendor
    2021-03-29T06:07:57.793+00:00

    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.

    0 comments No comments