Share via


DataTemplateSelector Class

Definition

Selects a DataTemplate based on the data object and container. Override OnSelectTemplate(Object, BindableObject) to implement selection logic.

public ref class DataTemplateSelector abstract : Microsoft::Maui::Controls::DataTemplate
public abstract class DataTemplateSelector : Microsoft.Maui.Controls.DataTemplate
type DataTemplateSelector = class
    inherit DataTemplate
Public MustInherit Class DataTemplateSelector
Inherits DataTemplate
Inheritance
DataTemplateSelector

Remarks

Application developers override the OnSelectTemplate(Object, BindableObject) 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 SelectTemplate(Object, BindableObject), 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;
}

Constructors

Name Description
DataTemplateSelector()

Properties

Name Description
Bindings

Gets the dictionary of bindings to apply to templated items.

(Inherited from DataTemplate)
LoadTemplate (Inherited from ElementTemplate)
Values

Gets the dictionary of property values to apply to templated items.

(Inherited from DataTemplate)

Methods

Name Description
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)
SelectTemplate(Object, BindableObject)

Returns a DataTemplate for the specified item and container.

SetBinding(BindableProperty, BindingBase)

Sets a binding for a property on templated items.

(Inherited from DataTemplate)
SetValue(BindableProperty, Object)

Sets a static value for a property on templated items.

(Inherited from DataTemplate)

Explicit Interface Implementations

Name Description
IDataTemplateController.Id (Inherited from DataTemplate)
IDataTemplateController.IdString (Inherited from DataTemplate)

Extension Methods

Name Description
CreateContent(DataTemplate, Object, BindableObject)

Selects the appropriate template and creates its content for the specified item.

SelectDataTemplate(DataTemplate, Object, BindableObject)

Returns the appropriate template, invoking selector logic if the template is a DataTemplateSelector.

SetBinding(DataTemplate, BindableProperty, String)

Creates a binding on the template for the specified property and path.

Applies to