Converters

  • 4 minutes to read

The DevExpress WinUI suite has a set of extended value converters that help you perform conversions between different types and even between values of the same type. These converters are useful when binding one property to another of an incompatible type (for instance, when binding a control's Boolean property to a property of the Visibility type).

All converters are in the DevExpress.WinUI.Mvvm.UI namespace.

ObjectToObjectConverter

This converter acts as a dictionary, which maps predefined input values to output values of any type.

ObjectToObjectConverter has three properties.

  • Map – this property contains a collection of MapItem objects that map input and output values.
  • DefaultTarget – this property contains an object, which should be returned in case of a failed conversion.
  • DefaultSource – the property is used in a backward conversion. It is returned when the conversion fails.

See the following example that explains how to use the ObjectToObjectConverter. Assume that there is a View Model exposing a property of the ColorState enumeration type.

public enum ColorState { Red, Green, Yellow }
public class ViewModel {
    public ColorState State { get; set; }
}

A View bound to the View Model contains a TextBlock, which should display "Ready", "Steady", "Go" if the ViewModel.State property equals "Red", "Yellow", and "Green" respectively. This value mapping can be done with the ObjectToObjectConverter.

<UserControl ...
            xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI">

    <UserControl.DataContext> 
        <ViewModels:ViewModel State="Green"/> 
    </UserControl.DataContext> 
    <UserControl.Resources> 
        <dxmvvm:ObjectToObjectConverter x:Key="ColorStateToStringConverter"> 
                <dxmvvm:MapItem Source="Red" Target="Ready"/> 
                <dxmvvm:MapItem Source="Yellow" Target="Steady"/> 
                <dxmvvm:MapItem Source="Green" Target="Go"/> 
        </dxmvvm:ObjectToObjectConverter> 
    </UserControl.Resources> 
    <Grid> 
        <TextBlock Text="{Binding State, Converter={StaticResource ColorStateToStringConverter}}"/> 
    </Grid>
</UserControl>

BooleanToObjectConverter

Converts the input Boolean, nullable Boolean or DefaultBoolean value to a value of any type.

This converter has three mapping properties: TrueValue, FalseValue, and NullValue. These properties specify objects that should be returned if the input value is True, False, or null (or DefaultBoolean.Default), respectively.

public class ViewModel {
    public bool IsEnabled { get; set; }
}
<UserControl ...
            xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI">

    <UserControl.DataContext> 
        <ViewModels:ViewModel IsEnabled="True"/> 
    </UserControl.DataContext> 
    <UserControl.Resources> 
        <dxmvvm:BooleanToObjectConverter x:Key="BooleanToColorConverter"> 
            <dxmvvm:BooleanToObjectConverter.TrueValue> 
                <SolidColorBrush Color="Green"/> 
            </dxmvvm:BooleanToObjectConverter.TrueValue> 
            <dxmvvm:BooleanToObjectConverter.FalseValue> 
                <SolidColorBrush Color="Red"/> 
            </dxmvvm:BooleanToObjectConverter.FalseValue> 
        </dxmvvm:BooleanToObjectConverter> 
    </UserControl.Resources> 
    <Grid> 
        <Border Background="{Binding IsEnabled, Converter={StaticResource BooleanToColorConverter}}"/> 
    </Grid>
</UserControl>

FormatStringConverter

This converter formats a value according to a specific format string. You can use the StringFormat property to implement formatting for a standard binding. However, bindings always use the default culture for the language defined in XAML. If it is necessary to use a custom culture, use the FormatStringConverter.

public class ViewModel {
    public int Price { get; set; }
}
<UserControl ...
            xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI">

    <UserControl.DataContext> 
        <ViewModels:ViewModel Price="32"/> 
    </UserControl.DataContext> 
    <UserControl.Resources> 
        <dxmvvm:FormatStringConverter x:Key="FormatStringConverter" FormatString="c"/>  
    </UserControl.Resources> 
    <Grid> 
        <TextBlock Text="{Binding Price, Converter={StaticResource FormatStringConverter}}"/> 
    </Grid>
</UserControl>

BooleanToVisibilityConverter

An extended version of the standard converter that maps Boolean values to the values of the Visibility type and vice versa.

Use the Inverse property to invert the conversion.

<UserControl ...
            xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI">
    <UserControl.Resources> 
        <dxmvvm:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </UserControl.Resources> 

    <CheckBox x:Name="straightConversionCheckBox" IsChecked="True">Is visible</CheckBox>
    <Button Visibility="{Binding IsChecked, ElementName=straightConversionCheckBox, 
            Converter={StaticResource BooleanToVisibilityConverter}}">
            Test Button
    </Button>
</UserControl>                

StringToVisibilityConverter

Converts string values to the Visibility type.

If the input value is Null or String.Empty, the converter returns Visibility.Collapsed; otherwise, it returns Visibility.Visible.

Use the Inverse property to invert the conversion.

NumericToBooleanConverter

Converts numeric values to the Boolean type.

If the input value is 0, the converter returns False; otherwise, it returns True.

Backward conversion is not supported.

NumericToVisibilityConverter

Converts numeric values to the Visibility type.

If the input value is 0, the converter returns Visibility.Collapsed; otherwise, it returns Visibility.Visible.

Backward conversion is not supported.

Use the Inverse property to invert the conversion.

Below is an example of how to use this converter.

<UserControl ...
            xmlns:dxmvvm="using:DevExpress.WinUI.Mvvm.UI">

    <UserControl.Resources> 
        <dxmvvm:NumericToVisibilityConverter x:Key="NumericToVisibilityConverter"/>
    </UserControl.Resources> 
    <Grid> 
        <ItemsControl ItemsSource="{Binding Items}" Visibility="{Binding Items.Count, Converter={StaticResource NumericToVisibilityConverter}}"/> 
    </Grid>
</UserControl>

BooleanNegationConverter

Inverts the input Boolean or DefaultBoolean value.

DefaultBooleanToBooleanConverter

Converts between the three-state DefaultBoolean type (Default, True and False) and the nullable Boolean type (null, True, and False).

ObjectToBooleanConverter

If the input object is null, the converter returns False; otherwise, it returns True.

Backward conversion is not supported.

StringToBooleanConverter

If the input string is empty or null, the converter returns False; otherwise, it returns True.

Backward conversion is not supported.