How can I help you?
DataTemplateSelector in Xamarin Picker (SfPicker)
8 Jan 202512 minutes to read
SfPicker supports DataTemplateSelector which you can choose a DataTemplate based on the data object.
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="DefaultTemplate">
<Grid >
<Label Text="{Binding LanguageName}" FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Center"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="SepcificTemplate">
<Grid BackgroundColor="Green">
<Label HorizontalOptions="CenterAndExpand" FontSize="Medium" VerticalOptions="CenterAndExpand" Text="{Binding LanguageName}"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid>
<syncfusion:SfPicker
ItemsSource="{Binding LanguageCollection }" >
<syncfusion:SfPicker.ItemTemplate> <local:DataTemplateSelectorViewModel DefaultTemplate="{StaticResource DefaultTemplate}" SpecificTemplate="{StaticResource SepcificTemplate}"/>
</syncfusion:SfPicker.ItemTemplate>
</syncfusion:SfPicker>
</Grid>
</Grid>
</Grid>
</ContentPage.Content>OnSelectTemplate
The OnSelectTemplate is a overridden method to return a particular DataTemplate, which shown in the following code:
public class DataTemplateSelectorViewModel : DataTemplateSelector
{
private DataTemplate defaulttemplate;
public DataTemplate DefaultTemplate
{
get { return defaulttemplate; }
set { defaulttemplate = value; }
}
private DataTemplate specifictemplate;
public DataTemplate SpecificTemplate
{
get { return specifictemplate; }
set { specifictemplate = value; }
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var message = item as Model;
if (message == null)
return null;
return message.IsPremium ? SpecificTemplate : DefaultTemplate;
}
}The following screenshot illustrates the output of above code.

We have attached sample for reference. You can download the sample from the following link.
Sample link:DataTemplateSelectorSample
NOTE
You can refer to our Xamarin Picker feature tour page for its groundbreaking feature representations. You can also explore our Xamarin.Forms Picker example to knows the functionalities of each feature.