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:
publicenum ColorState { Red, Green, Yellow }
publicclass 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:ViewModelState="Green"/>
</UserControl.DataContext>
<UserControl.Resources>
<dxmvvm:ObjectToObjectConverterx:Key="ColorStateToStringConverter">
<dxmvvm:MapItemSource="Red"Target="Ready"/>
<dxmvvm:MapItemSource="Yellow"Target="Steady"/>
<dxmvvm:MapItemSource="Green"Target="Go"/>
</dxmvvm:ObjectToObjectConverter>
</UserControl.Resources>
<Grid>
<TextBlockText="{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.
publicclass ViewModel {
publicbool IsEnabled { get; set; }
}
<UserControl.DataContext>
<ViewModels:ViewModelIsEnabled="True"/>
</UserControl.DataContext>
<UserControl.Resources>
<dxmvvm:BooleanToObjectConverterx:Key="BooleanToColorConverter">
<dxmvvm:BooleanToObjectConverter.TrueValue>
<SolidColorBrushColor="Green"/>
</dxmvvm:BooleanToObjectConverter.TrueValue>
<dxmvvm:BooleanToObjectConverter.FalseValue>
<SolidColorBrushColor="Red"/>
</dxmvvm:BooleanToObjectConverter.FalseValue>
</dxmvvm:BooleanToObjectConverter>
</UserControl.Resources>
<Grid>
<BorderBackground="{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.
publicclass ViewModel {
publicint Age { get; set; }
}
<UserControl.DataContext>
<ViewModels:ViewModelAge="32"/>
</UserControl.DataContext>
<UserControl.Resources>
<dxmvvm:FormatStringConverterx:Key="FormatStringConverter"FormatString="Age: {0}"/>
</UserControl.Resources>
<Grid>
<TextBlockText="{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:NumericToVisibilityConverterx:Key="NumericToVisibilityConverter"/>
</UserControl.Resources>
<Grid>
<ItemsControlItemsSource="{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:
- Getting Started with DevExpress MVVM Framework. Commands and View Models.
- DevExpress MVVM Framework. Introduction to Services, DXMessageBoxService and DialogService.
- DevExpress MVVM Framework. Interaction of ViewModels. IDocumentManagerService.
- DevExpress MVVM Framework. Introduction to POCO ViewModels.
- DevExpress MVVM Framework. Interaction of ViewModels. Messenger.
- DevExpress MVVM Framework. Using Scaffolding Wizards for building Views.
- DevExpress MVVM Framework. Data validation. Implementing IDataErrorInfo.
- DevExpress MVVM Framework. Using DataAnnotation attributes and DevExpress Fluent API.
- DevExpress MVVM Framework. Behaviors.
- DevExpress MVVM Framework. TaskbarButtonService, ApplicationJumpListService and NotificationService.
- DevExpress MVVM Framework. Asynchronous Commands.
- THIS POST: DevExpress MVVM Framework. Converters.