Bagikan melalui


DataTemplateSelector Class

Definition

Selects DataTemplate objects by data type and container.

public abstract class DataTemplateSelector : Xamarin.Forms.DataTemplate
type DataTemplateSelector = class
    inherit DataTemplate
Inheritance
System.Object
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.

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

DataTemplateSelector()

Creates a new DataTemplateSelector with default values.

Properties

Bindings

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

(Inherited from DataTemplate)
Values

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

(Inherited from DataTemplate)

Methods

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)

The developer overrides this method to return a valid data template for the specified item. This method is called by SelectTemplate(Object, BindableObject).

SelectTemplate(Object, BindableObject)

Returns a DataTemplate for item by calling OnSelectTemplate(Object, BindableObject) and verifying its result.

SetBinding(BindableProperty, BindingBase)

Sets the binding for property.

(Inherited from DataTemplate)
SetValue(BindableProperty, Object)

Sets the value of property.

(Inherited from DataTemplate)

Explicit Interface Implementations

IDataTemplate.LoadTemplate

For internal use only.

(Inherited from ElementTemplate)
IDataTemplateController.Id (Inherited from DataTemplate)
IDataTemplateController.IdString (Inherited from DataTemplate)

Extension Methods

CreateContent(DataTemplate, Object, BindableObject)

For internal use by the Xamarin.Forms platform.

SelectDataTemplate(DataTemplate, Object, BindableObject)

For internal use by the Xamarin.Forms platform.

SetBinding(DataTemplate, BindableProperty, String)

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

Applies to