Get Started with Xamarin.Forms DataTemplateSelector | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)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  (508)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  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)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

Introduction

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="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="onlineTemplate">
               <ViewCell>
                 <Label Text={Binding Name} TextColor="Green"/>
               </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="offlineTemplate">
               <ViewCell>
                 <Label Text={Binding Name} TextColor="Orange"/>
               </ViewCell>
            </DataTemplate>
            <local:StatusDataTemplateSelector x:Key="statusDataTemplateSelector"
                OnlineTemplate="{StaticResource onlineTemplate}"
                OfflineTemplate="{StaticResource offlineTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>
</ContentPage>

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

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

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

Conclusion

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!

Tags:

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