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.
