Contents
- Create and Initialize Business Models
- OnSelectTemplate
Having trouble getting help?
Contact Support
Contact Support
DataTemplateSelector in Xamarin ComboBox (SfComboBox)
22 Aug 202211 minutes to read
SfComboBox
supports DataTemplateSelector, which is used to choose a DataTemplate based on data object.
<ContentPage.BindingContext>
<local:MobileDetailViewModel/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="default">
<ViewCell>
<Grid Padding="5">
<Label Text="{Binding Mobile}" TextColor="Green"/>
</Grid>
</ViewCell>
</DataTemplate>
<DataTemplate x:Key="specific">
<ViewCell>
<Grid Padding="5">
<Label Text="{Binding Mobile}" BackgroundColor="LightGray" TextColor="Red"/>
</Grid>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout VerticalOptions="Start" HorizontalOptions="Start" Padding="30">
<comboBox:SfComboBox DataSource="{Binding MobileCollection}" DisplayMemberPath="Mobile">
<comboBox:SfComboBox.ItemTemplate>
<local:DataTemplateSelectorViewModel DefaultTemplate="{StaticResource default}" SpecificDataTemplate="{StaticResource specific}" />
</comboBox:SfComboBox.ItemTemplate>
</comboBox:SfComboBox>
</StackLayout>
public partial class MainPage : ContentPage
{
DataTemplate defaultTemplate;
DataTemplate specifictempalte;
public MainPage()
{
InitializeComponent();
MobileDetailViewModel mobileDetailViewModel = new MobileDetailViewModel();
defaultTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.Padding = new Thickness(5);
Label label = new Label();
label.SetBinding(Label.TextProperty, "Mobile");
label.TextColor = Color.Green;
grid.Children.Add(label);
return new ViewCell { View = grid };
});
specifictempalte = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.Padding = new Thickness(5);
Label label = new Label();
label.SetBinding(Label.TextProperty, "Mobile");
label.BackgroundColor = Color.LightGray;
label.TextColor = Color.Red;
grid.Children.Add(label);
return new ViewCell { View = grid };
});
StackLayout layout = new StackLayout()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Padding = new Thickness(30)
};
SfComboBox comboBox = new SfComboBox();
comboBox.HeightRequest = 40;
comboBox.DataSource = mobileDetailViewModel.MobileCollection;
this.BindingContext = mobileDetailViewModel;
comboBox.DisplayMemberPath = "Mobile";
comboBox.ItemTemplate = new DataTemplateSelectorViewModel { DefaultTemplate = defaultTemplate, SpecificDataTemplate = specifictempalte };
layout.Children.Add(comboBox);
Content = layout;
}
}
Create and Initialize Business Models
Define a simple model class MobileDetail with fields IsAvailableInStock, Mobile and populate mobile detail in ViewModel.
public class MobileDetailViewModel
{
public ObservableCollection<MobileDetail> MobileCollection { get; set; }
public MobileDetailViewModel()
{
this.MobileCollection = new ObservableCollection<MobileDetail>()
{
new MobileDetail () { Mobile="Samasung S8", IsAvailableInStock=false },
new MobileDetail () { Mobile="Samasung S9", IsAvailableInStock=true },
new MobileDetail () { Mobile="Samsung S10", IsAvailableInStock=true },
new MobileDetail () { Mobile="Samsung S10 plus", IsAvailableInStock=true },
new MobileDetail () { Mobile="iPhone 7", IsAvailableInStock=true },
new MobileDetail () { Mobile="iPhone 8", IsAvailableInStock=true },
new MobileDetail () { Mobile="iPhone X", IsAvailableInStock=false },
new MobileDetail () { Mobile="iPhone XR", IsAvailableInStock=false },
new MobileDetail () { Mobile="iPhone XS", IsAvailableInStock=true },
};
}
}
public class MobileDetail
{
public string Mobile { get; set; }
public bool IsAvailableInStock { get; set; }
}
OnSelectTemplate
The OnSelectTemplate is an overridden method to return a particular DataTemplate. The following code sample demonstrates how to use the OnSelectTemplate method.
public class DataTemplateSelectorViewModel : DataTemplateSelector
{
public DataTemplate DefaultTemplate { get; set; }
public DataTemplate SpecificDataTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var message = item as MobileDetail;
if (message.IsAvailableInStock == null)
return null;
return message.IsAvailableInStock == false ? SpecificDataTemplate : 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: SfComboBox_DataTemplateSelector