Converters between different types of data

Classes

  ClassDescription
Public classAlwaysTrueConverter
This converter always converts to the true value. The ConvertBack(Object, Type, Object, CultureInfo) method returns DoNothing
Public classBoolToVisibilityConverter
Converts boolean value to the Visibility type.
Public classConverterAttribute
Enables to declare standard string patterns to convert values in cells.
Public classConverterBaseAttribute
Base class for all converter attributes. Simplifies IValueConverter declaration in properties of user-defined classes.
Public classDoubleConverter
Converts Double values to String vice-versa.
Public classDoubleConverterAttribute
Attribute to declare the DoubleConverter in a property of user-defined class.
Public classEmptyConverter
Converts any value to Empty string.
Public classEmptyConverterAttribute
Declares EmptyConverter in a property of a user-defined class.
Public classGenericValueConverter
Generic converter tries to convert value to String and vice-versa
Public classStringConverter
Converts values to String using standard string patterns.

Remarks

Data formatting means transformation of values returned by data objects (usually by their properties) to strings for displaying in grid cells. Data formatting should not be confused with templates that can be used to change data presentation in cells. Formatting means simple transformation of unformatted values to text. The easiest way to transform value to text is to call Object.ToString() method. However it is not always convenient as it can not be to display the same value differently in different parts of application and poorly implements the principle of separating data from presentation. The most general method is to use converters.

MVVM data model implies separation of data from presentation with data binding. In this model data can be presented in different ways in different controls. Data binding enables binding business logic with graphical controls on data layer. When controls are initialized, the data layer provides values that are later transformed to required types and displayed on the presentation layer. User may change values with corresponding controls (combo box, text box, etc). New value entered by user is transformed to the required type and transmitted to the data layer via data binding. Type conversion is done via classes that implement IValueConverter interface. An example of implementing a data converter that transforms business logic values to strings is provided below.

 Copy imageCopy
internal class IntToStringConverter : IValueConverter
{
    // Converts int-type value to string 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is int)
        {
            return string.Format(culture, "{0}", value);
        }
        return null;
    }
    // Parses string to int-type value 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string s = value as string;
        if (!string.IsNullOrEmpty(s))
        {
            return int.Parse(s, culture);
        }
        return Binding.DoNothing;
    }
}
IValueConverter may actually convert business logic to any data types. We will be most interested in conversion to String and back. The grid uses this conversion method to display rows in grid cells.

Programmer has the following ways to set type converters.

 Copy imageCopy
class MyDataObject
{
    //Specific converter to present double values as strings with 2 digits after dot 
    [DoubleConverter(2)]
    public double SomeDoubleValue { get; set; }

    //Generic converter to format value with the specified format 
    [Converter("yyyy-MM-dd HH:mm:ss")]
    public DateTime SomeDateValue { get; set; }
}

If converters provided by Dapfor's framework are not sufficient, programmers may create their own converters and attributes to use in declarative formatting. An example of implementing an attribute for declarative setting of the above shown IntToStringConverter:

 Copy imageCopy
public class IntToStringConverterAttribute : ConverterBaseAttribute
{
    private readonly IntToStringConverter _converter;

    public IntToStringConverterAttribute()
    {
        _converter = new IntToStringConverter();
    }

    public override IValueConverter Converter
    {
        get
        {
            return _converter;
        }
    }
}

As we can see, IntToStringConverterAttribute create an instance of IntToStringConverter and returns it in a virtual property. IntToStringConverterAttribute class can also be enhanced by builders with parameters for sending them to IntToStringConverter during initialization. An example of using the attribute created above is shown below:

 Copy imageCopy
class MyDataObject
{
    //...  
    [IntToStringConverter]
    public int SomeIntValue { get; set; }
}

Declarative formatting has one important feature – if a grid displays data of different types in the same column, and if no converters are used in this column, data in grid cells may be displayed differently depending on converters set in data object properties.

Another way to use converters is to set them directly in grid columns. This method is used in cases when usage of declarative formatting is not desirable or when it is necessary to display data in different grids differently. The below example demonstrates usage of converters in grid columns.

 Copy imageCopy
GridControl grid = ...; 
grid.Headers[0]["SomeDoubleValue"].ValueConverter = new DoubleConverter(2);