IValueConverter is an interface defined in Microsoft .NET XAML for use in WPF, Silverlight and Xamarin.Forms. It is used in data binding to convert data from one format to another. The conversion can be one-way or bidirectional.
The .NET Binding
classes have an optional Converter property of type IValueConverter
which can be used to convert data from its native internal representation to something more convenient to a specific UI objective. A DateTime
object could be converted to a string representation in a particular date/time display format, for example, or a numeric value could be converted to a color value for the background of a status control.
IValueConverter
defines two methods: Convert
and ConvertBack
. Implement Convert
to convert the source data to the display data representation. Implement ConvertBack
to convert the display data representation to the source data representation. This is useful when the UI allows the user to edit the data. In the DateTime
example above, if the display data were bound to a TextBox
edit control (and the data binding Mode is set to TwoWay), the user could type in or change the date/time values. The IValueConverter
specified on the data binding would be responsible for converting the text the user entered into a DateTime
value in its ConvertBack
method.
For more information, see the MSDN documentation on the IValueConverter interface.