ConverterAttribute

There are cases when the editors require types that are not the same as the property type defined in the business object. In these case users can use a converter class. This is a custom class implementing the IPropertyConverter interface. The next example demonstrates how such converted can be used.

Example

In this example we will demonstrate how we can use a number picker editor, which supports double type, for property of type decimal. First, you need a proper converter:

class DecimalToDoublePropertyConverter : IPropertyConverter
{
    public object Convert(object value)
    {
        return System.Convert.ToDouble((decimal)value);
    }

    public object ConvertBack(object value)
    {
        return System.Convert.ToDecimal((double)value);
    }
}

Here is the decoration of the source class with attributes:

public class Item : NotifyPropertyChangedBase
{
    decimal price;

    [DisplayOptions(Header = "Name")]
    public string Name { get; set; } = "vase";

    [DisplayOptions(Header = "Price")]
    [Converter(typeof(DecimalToDoublePropertyConverter))]
    public decimal Price
    {
        get
        {
            return this.price;
        }
        set
        {
            if (this.price != value)
            {
                this.price = value;
                this.OnPropertyChanged();
            }
        }
    }
}

Finally, you have to register a number picker editor for the item Price property:

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

dataForm.RegisterEditor("Price", EditorType.NumberPickerEditor);