New to Telerik UI for Xamarin? Download free 30-day trial

Data Source

Data Class

One way to set data form source is to use a class and decorate its properties with data annotations. These data annotations are used to build metadata for each property used by the data form to customize its UI. Here are listed all available data annotations:

Example

Here is a sample class decorated with annotations:

public class Customer
{
    [DisplayOptions(Group = "Basic Info", PlaceholderText = "First name", Header = "First Name")]
    [NonEmptyValidator("Please fill the first name", "OK")]
    public string FirstName { get; set; }

    [DisplayOptions(Group = "Basic Info", PlaceholderText = "Last Name", Header = "Last Name")]
    [NonEmptyValidator("Please fill the last name", "OK")]
    public string LastName { get; set; }

    [DisplayOptions(Group = "Additional Info", Header = "Age")]
    [NumericalRangeValidator(0, 100)]
    public int Age { get; set; }
}

Here is how to set an instance of the class as data form source:

var dataForm = new RadDataForm
{
    Source = new Customer()
};

Third Party Data Source

Sometimes the source is not an instance of a specific class that can be decorated with data annotations. In these cases you have to provide your own metadata through the RadDataForm.MetadataProvider property.

Example

This example will demonstrate how to use a dictionary as data form source.

var source = new Dictionary<string, string>();

source.Add("FirstName", "John");
source.Add("LastName", "Dow");
source.Add("Country", "Unknown");

You have to set a custom metadata provider that will provide information for each property, in this case dictionary item. The data form will use this metadata when building its UI.

The metadata provider has to inherit from the PropertyMetadataProviderBase class:

public class CustomMetadataProvider : PropertyMetadataProviderBase
{
    private readonly List<IEntityProperty> entityProperties = new List<IEntityProperty>();

    public override List<IEntityProperty> EntityProperties
    {
        get
        {
            return entityProperties;
        }
    }

    public override void Initialize(object source)
    {
        var sourceDictionary = source as Dictionary<string, string>;

        foreach (var item in sourceDictionary)
        {
            var metadata = new EntityPropertyMetadata
            {
                Header = item.Key
            };

            var property = new CustomEntityProperty(item.Key, sourceDictionary, metadata);

            this.entityProperties.Add(property);
        }
    }
}

In the Initialize method you have to add the metadata for each dictionary item using the EntityPropertyMetadata class. It contains all metadata information that can be set on a property and you can modify it as per your needs.

You also need a custom entity property class that is responsible for committing the values entered in the data form:

public class CustomEntityProperty : IEntityProperty
{
    private readonly Dictionary<string, string> dataItem;

    private readonly string propertyName;
    private readonly EntityPropertyMetadata metadata;

    public CustomEntityProperty(string propertyName, Dictionary<string, string> item, EntityPropertyMetadata metadata)
    {
        this.metadata = metadata;
        this.propertyName = propertyName;
        this.dataItem = item;
    }

    public object OriginalValue
    {
        get
        {
            return this.dataItem[propertyName];
        }
    }

    public EntityPropertyMetadata Metadata
    {
        get
        {
            return this.metadata;
        }
    }

    public object DataItem
    {
        get
        {
            return this.dataItem;
        }
    }

    public string PropertyName
    {
        get
        {
            return this.propertyName;
        }
    }

    public object PropertyValue { get; set; }

    public Type PropertyType
    {
        get
        {
            return typeof(string);
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Commit()
    {
        this.dataItem[propertyName] = (string)this.PropertyValue;
    }
}

Here is the data form setup:

dataForm.MetadataProvider = new CustomMetadataProvider();
dataForm.Source = source;

See Also

In this article