Mapping Converters

  • 3 minutes to read

Mapping converters allow you to apply custom logic to mapping.


Converters can change data from one type to another, translate data based on additional information, or change the format in which the data is stored. To associate a value converter with a mapping, create a class that implements the IValueConverter interface and its Convert and ConvertBack methods and specify it in XAML using the Mapping.Converter property.

The data is propagated back and forth between the property, default mapping method and a data source field. You can insert a converter between the property and the default mapping method, or between default mapping method and the data source field, or instead of a default mapping method. Converter position in that propagation chain is defined by the MappingConversionBehavior enumeration.

The following image illustrates the converter's processing order:


How to Create a Converter

This section demonstrates how a mapping converter can solve a particular problem.

Problem: When the AppointmentLabelMappings.ColorSavingType is set to DXColorSavingType.ColorString, the color is stored as hex string in the "0xARGB" format. However, HTML color codes cannot be used to specify the label colors because they use the ARGB format with a hash (#) prefix.

Solution: Implement a custom converter to store colors as HTML color codes when the color saving type is ColorString.

  1. Implement a class with a IValueConverter interface (inherited from the MarkupExtension class).

    Friend Class HtmlColorCodeToHexConverter
        Inherits MarkupExtension
        Implements IValueConverter
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
            If (value.GetType() Is GetType(String)) AndAlso value.ToString().StartsWith("#") Then
                Return String.Concat("0x", value.ToString().Remove(0,1))
                Return value
            End If
        End Function
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            If (value.GetType() Is GetType(String)) AndAlso value.ToString().StartsWith("0x") Then
                Return String.Concat("#", value.ToString().Remove(0, 2))
                Return value
            End If
        End Function
        Public Overrides Function ProvideValue(ByVal serviceProvider As IServiceProvider) As Object
            Return Me
        End Function
    End Class
  2. Assign the converter to the label color mapping's Mapping.Converter property. Set the Mapping.ConversionBehavior to the MappingConversionBehavior.BetweenFieldAndMapping value so that it provides "0xARGB" formatted data from the data source to the default mapping and converts data obtained from the default mapping to the "#ARGB" format before passing it to the data source.

        <dxsch:AppointmentLabelMappings Color="{dxsch:Mapping FieldName=ColorData, 
            Converter= {local:HtmlColorCodeToHexConverter}}"                            
            Id="Id" />