DevExpress MVVM Framework. Converters.

WPF Team Blog
19 August 2014

DevExpress MVVM Framework includes a variety of value converters that convert between values of the same type as well as between different types. Converters are useful when binding properties of incompatible types (for instance, when binding a UserControl property of type Boolean to a property of type Visibility).

Our converters are available from the DevExpress.Mvvm.UI namespace.

The features described in this post are in both the full DevExpress MVVM framework included with our WPF Subscription, and the free stand-alone version on nuget and github.


ObjectToObjectConverter

ObjectToObjectConverter uses a dictionary to map specific input values to output values of a useful type.

ObjectToObjectConverter has three properties:

  • Map – a collection of MapItem objects which pair an input value to its output
  • DefaultTarget – an object returned when the conversion fails
  • DefaultSource – used in reverse direction conversion in a similar manner to DefaultTarget

We can use an example to see how the ObjectToObjectConverter works. Let’s assume we have a View Model which exposes a property of the ColorState enumeration type:

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

This View Model is bound to a View which includes a TextBlock that reads “Ready”, “Steady” or “Go” when the ViewModel.State property is “Red”, “Yellow” or “Green” respectively. We can describe this mapping with an ObjectToObjectConverter.

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


BooleanToObjectConverter

BooleanToObjectConverter converts an input value of type Boolean, nullable Boolean or DefaultBoolean to a value of an arbitrary type.

This converter provides three properties to specify a mapping: TrueValue, FalseValue, and NullValue. These property values are objects returned for input that is True, False and null or DefaultBoolean.Default respectively.

public class ViewModel {
    public bool IsEnabled { get; set; }
}
<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> 


FormatStringConverter

FormatStringConverter outputs a value using a defined format string. The WPF binding system provides its own StringFormat property to enable this feature. However, bindings pick up the default culture of the language defined in XAML. Our FormatStringConverter accepts any custom culture.

public class ViewModel {
    public int Age { get; set; }
}
<UserControl.DataContext> 
    <ViewModels:ViewModel Age="32"/> 
</UserControl.DataContext> 
<UserControl.Resources> 
    <dxmvvm:FormatStringConverter x:Key="FormatStringConverter" FormatString="Age: {0}"/>  
</UserControl.Resources> 
<Grid> 
    <TextBlock Text="{Binding Age, Converter={StaticResource FormatStringConverter}}"/> 
</Grid> 


BooleanToVisibilityConverter

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

Two additional properties are available in this converter:

  • Inverse – when True, negates the input Boolean before applying the conversion
  • HiddenInsteadOfCollapsed – when True, the converter uses the Hidden visibility state in place of the default Collapsed.


NumericToBooleanConverter

NumericToBooleanConverter converts numeric values to the Boolean type.
If the input value is 0, the converter returns False;
otherwise, it returns True.

NumericToBooleanConverter does not support conversion in the reverse direction.


NumericToVisibilityConverter

NumericToVisibilityConverter converts numeric values to the Visibility type.
If the input value is 0, the converter returns Visibility.Collapsed;
otherwise, it returns Visibility.Visible.

NumericToVisibilityConverter does not allow conversion in the reverse direction.

Two additional properties are available in this converter:

  • Inverse – when True, a non-zero value in place of 0 (or 0 in place of a non-zero value) is passed as input into the converter
  • HiddenInsteadOfCollapsed – when True, the converter uses the Hidden state in place of the default Collapsed

Below is an example of how to use this converter.

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


BooleanNegationConverter

Returns the logical negative of the input Boolean or DefaultBoolean value.


DefaultBooleanToBooleanConverter

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


ObjectToBooleanConverter

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

ObjectToBooleanConverter does not allow conversion in the reverse direction


StringToBooleanConverter

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

StringToBooleanConverter does not allow conversion in the reverse direction.



OTHER RELATED ARTICLES:

  1. Getting Started with DevExpress MVVM Framework. Commands and View Models.
  2. DevExpress MVVM Framework. Introduction to Services, DXMessageBoxService and DialogService.
  3. DevExpress MVVM Framework. Interaction of ViewModels. IDocumentManagerService.
  4. DevExpress MVVM Framework. Introduction to POCO ViewModels.
  5. DevExpress MVVM Framework. Interaction of ViewModels. Messenger.
  6. DevExpress MVVM Framework. Using Scaffolding Wizards for building Views.
  7. DevExpress MVVM Framework. Data validation. Implementing IDataErrorInfo.
  8. DevExpress MVVM Framework. Using DataAnnotation attributes and DevExpress Fluent API.
  9. DevExpress MVVM Framework. Behaviors.
  10. DevExpress MVVM Framework. TaskbarButtonService, ApplicationJumpListService and NotificationService.
  11. DevExpress MVVM Framework. Asynchronous Commands.
  12. THIS POST: DevExpress MVVM Framework. Converters.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.