DataTemplateSelector DataTemplateSelector Class


Selects DataTemplate objects by data type and container.

public abstract class DataTemplateSelector : Xamarin.Forms.DataTemplate
type DataTemplateSelector = class
    inherit DataTemplate


Application developers override the Xamarin.Froms.DataTemplateSelector.OnSelectTemplate method to return a unique DataTemplate for a data type and parent container combination. Additionally, because the same exact template instance must be returned for a given piece of data across successive calls to Xamarin.Froms.DataTemplateSelector.SelectTemplate, developers should create and store these DataTemplate in their constructor overrides.

Developers should note the following items:
  • OnSelectTemplate must not return another DataTemplateSelector, and
  • The Android platform is limited to 20 templates per list view.
The following example shows a basic use:

class MyDataTemplateSelector : DataTemplateSelector
    public MyDataTemplateSelector ()
        // Retain instances
        this.templateOne = new DataTemplate (typeof (ViewA));
        this.templateTwo = new DataTemplate (typeof (ViewB));

    protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
        if (item is double)
            return this.templateOne;
        return this.templateTwo;

    private readonly DataTemplate templateOne;
    private readonly DataTemplate templateTwo;


DataTemplateSelector() DataTemplateSelector()

Creates a new DataTemplateSelector with default values.


CreateContent() CreateContent()

Used by the XAML infrastructure to load data templates and set up the content of the resulting UI.

(Inherited from ElementTemplate)
OnSelectTemplate(Object, BindableObject) OnSelectTemplate(Object, BindableObject)

The developer overrides this method to return a valid data template for the specified item. This method is called by Xamarin.Forms.DataTemplateSelector.SelectTemplate.

SelectTemplate(Object, BindableObject) SelectTemplate(Object, BindableObject)

Returns a DataTemplate for item by calling Xamarin.Forms.DataTemplateSelector.OnSelectTemplate and verifying its result.

SetBinding(BindableProperty, BindingBase) SetBinding(BindableProperty, BindingBase)

Sets the binding for property.

(Inherited from DataTemplate)
SetValue(BindableProperty, Object) SetValue(BindableProperty, Object)

Sets the value of property.

(Inherited from DataTemplate)


Bindings Bindings

Gets a dictionary of bindings, indexed by the bound properties.

(Inherited from DataTemplate)
IDataTemplate.LoadTemplate IDataTemplate.LoadTemplate

For internal use only.

(Inherited from ElementTemplate)
Values Values

Returns a dictionary of property values for this DataTemplate, indexed by property.

(Inherited from DataTemplate)

Extension Methods

CreateContent(DataTemplate, Object, BindableObject) CreateContent(DataTemplate, Object, BindableObject)

For internal use by the Xamarin.Forms platform.

SelectDataTemplate(DataTemplate, Object, BindableObject) SelectDataTemplate(DataTemplate, Object, BindableObject)

For internal use by the Xamarin.Forms platform.

SetBinding(DataTemplate, BindableProperty, String) SetBinding(DataTemplate, BindableProperty, String)

Binds the self object's targetProperty to a new Binding instance that was created with path.

LoadFromXaml<TXaml>(TXaml, String) LoadFromXaml<TXaml>(TXaml, String)

Returns an initialized view by loading the specified xaml.

LoadFromXaml<TXaml>(TXaml, Type) LoadFromXaml<TXaml>(TXaml, Type)

Returns a TXaml with the properties that are defined in the application manifest for callingType.

Applies to