Get Started with Xamarin.Forms DataTemplateSelector | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (226)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (942)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (157)Chart  (140)Chart of the week  (54)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (70)Development  (658)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (614)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Get Started with Xamarin.Forms DataTemplateSelector


Most of the time, developers avoid items controls without template support.  DataTemplate support saves them from rewriting code. It is virtually a necessity that an items control should support data templates with DataTemplateSelector.

Xamarin.Forms DataTemplateSelector

The Xamarin.Forms DataTemplateSelector can apply DataTemplate with some validation. With it, you can apply a variety of data templates to items at runtime.

In our Xamarin.Forms controls, we provide almost all our items controls with DataTemplateSelector support. Controls such as ListView, DataGrid, and Kanban Board already have DataTemplateSelector support. In 2019 Volume 2, the Autocomplete, ComboBox, Picker, and Rotator controls will support the same.

In this blog, we’ll see how to apply Xamarin.Forms DataTemplateSelector to an items control.

How to apply Xamarin.Forms DataTemplateSelector

First, let’s see how to apply DataTemplateSelector to the Xamarin.Forms ListView.

Step 1: Create a class derived from DataTemplateSelector and an override OnSelectTemplate method. Return the desired DataTemplate based on validation.

public class StatusDataTemplateSelector : DataTemplateSelector
  public DataTemplate OnlineTemplate { get; set; }
  public DataTemplate OfflineTemplate { get; set; }

  protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
    return ((Employee)item).IsOnline ? OnlineTemplate : OfflineTemplate 

Step 2: Add the StatusDataTemplateSelector into the ResourceDictionary of the ContentPage’s resources.

<ContentPage xmlns="" xmlns:x="">
            <DataTemplate x:Key="onlineTemplate">
                 <Label Text={Binding Name} TextColor="Green"/>
            <DataTemplate x:Key="offlineTemplate">
                 <Label Text={Binding Name} TextColor="Orange"/>
            <local:StatusDataTemplateSelector x:Key="statusDataTemplateSelector"
                OnlineTemplate="{StaticResource onlineTemplate}"
                OfflineTemplate="{StaticResource offlineTemplate}" />

Step 3: Apply StatusDataTemplateSelector to ListView’s ItemTemplate property.

    <syncfusion:SfListView ItemTemplate="{StaticResource statusDataTemplateSelector}"
                           ItemsSource="{Binding Employees}"

Xamarin.Forms SfListView with DataTemplateSelector

Xamarin.Forms SfListView with DataTemplateSelector

In this brief example, we can see how Xamarin.Forms ListView reacts with DataTemplateSelector. Our other controls such as Kanban Board, Radial Menu, and DataGrid also work with the DataTemplateSelector.

DataTemplateSelector support in 2019 Volume 2

In our 2019 Volume 2 release, Essential Studio for Xamarin.Forms supports DataTemplateSelector for the following controls:

Xamarin.Forms Autocomplete

The Xamarin Autocomplete control suggests a drop-down list of items from a large volume of data depending on the user’s input characters. You can apply the Xamarin.Forms DataTemplateSelector to the drop-down items.

Xamarin.Forms Autocomplete with DataTemplateSelector

Xamarin.Forms Autocomplete with DataTemplateSelector

Xamarin.Forms ComboBox

The Xamarin ComboBox control is a text box component that allows users to choose an option from a list of predefined options. You can apply the Xamarin.Forms DataTemplateSelector to the drop-down items.

Xamarin.Forms ComboBox with DataTemplateSelector

Xamarin.Forms ComboBox with DataTemplateSelector

Xamarin.Forms Rotator

The Xamarin Rotator control is a UI that provides an interactive way to rotate images and navigate through a collection of views. You can apply the Xamarin.Forms DataTemplateSelector to the rotator items.

Xamarin.Forms Rotator with DataTemplateSelector

Xamarin.Forms Rotator with DataTemplateSelector

Xamarin.Forms Picker

The Xamarin Picker control is an item selector control, which can be opened as a dialog. You can apply the Xamarin.Forms DataTemplateSelector to the items.

Xamarin.Forms Picker with DataTemplateSelector

Xamarin.Forms Picker with DataTemplateSelector


In this blog post, we have walked through the Syncfusion Xamarin.Forms controls that support the Xamarin.Forms DataTemplateSelector. We invite you to check out all of our Xamarin.Forms controls in action by downloading our free evaluation. You can check out the project to get a hands-on experience on the Switch control from this GitHub locationExplore our samples available on Google Play and the Microsoft Store, and learn about the controls’ advanced features in our documentation.

If you have any questions or require clarifications about these controls and features, please let us know in the comments below. You can also contact us through our support forumDirect-Trac, or feedback portal. We are happy to assist you!


Share this post:

Comments (1)

[…] Get Started with Xamarin.Forms DataTemplateSelector (Selva Ganapathy Kathiresan) […]

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed