Skip to main content

Converters

  • 5 minutes to read

The DevExpress.Xpf.Core.v24.2 library provides 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.Mvvm.UI namespace.

ObjectToObjectConverter

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

ObjectToObjectConverter provides 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 the text “Ready”, “Steady”, “Go” if the ViewModel.State property equals “Red”, “Yellow”, “Green” values respectively. This value mapping can be done with the ObjectToObjectConverter.

<UserControl ...
            xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <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 provides three mapping properties: TrueValue, FalseValue, and NullValue. These properties specify objects that should be returned if the input value is True, False, null (or DefaultBoolean.Default) respectively.

public class ViewModel {
    public bool IsEnabled { get; set; }
}
<UserControl ...
            xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <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. The standard binding supports this feature via the StringFormat property. 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="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <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.

Two additional properties are available in this converter.

  • Inverse – allows you to invert the conversion.
  • HiddenInsteadOfCollapsed – if this property is True, the converter returns the Hidden state instead of the Collapsed state when the input value is False.
<UserControl ...
            xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <CheckBox x:Name="straightConversionCheckBox" IsChecked="True">Is visible</CheckBox>
    <Button Visibility="{Binding IsChecked, ElementName=straightConversionCheckBox, 
            Converter={dxmvvm: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.

Two additional properties are available in this converter.

  • Inverse – allows you to invert the conversion.
  • HiddenInsteadOfCollapsed – if this property is True, the converter returns the Hidden state instead of the Collapsed state when the input value is Null or String.Empty.

NumericToBooleanConverter

Converts numeric values to the Boolean type.

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

Back 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.

Back conversion is not supported.

Two additional properties are available in this converter.

  • Inverse – allows you to invert the conversion.
  • HiddenInsteadOfCollapsed – if this property is True, the converter returns the Hidden state instead of the Collapsed state when the input value is 0.

Below is an example of how to use this converter.

<UserControl ...
            xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <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.

Back conversion is not supported.

StringToBooleanConverter

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

Back conversion is not supported.

Delegate Converter Factory (MultiValueConverter)

The DelegateConverterFactory class provides a set of static methods to create IValueConverter and IMutliValueConverter instances based on passed delegates.

ItemsControlMouseEventArgsConverter

Converts MouseEventArgs to an item from the ItemsSource collection in data-bound controls.

The converter supports the following controls and item types out of the box:

  • FlipView
  • FlipViewItem
  • GridView
  • GridViewItem
  • ListBox
  • ListBoxItem
  • ListView
  • ListViewItem
  • Menu
  • MenuItem
  • TreeView
  • TreeViewItem
  • TreeViewItem
  • TreeViewItem
  • TabControl
  • TabItem
  • StatusBar
  • StatusBarItem

If you want to use ItemsControlMouseEventArgsConverter with the DevExpress controls (for example, the AccordionControl), specify the ItemType property as follows:

<dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding MouseDoubleClickCommand}" PassEventArgsToCommand="True">
    <dxmvvm:EventToCommand.EventArgsConverter>
            <dxmvvm:ItemsControlMouseEventArgsConverter ItemType="{x:Type dxa:AccordionItem}" />
    </dxmvvm:EventToCommand.EventArgsConverter>
</dxmvvm:EventToCommand>
See Also