Value converters are used to display values in a more meaningful manner in WPF. Value converters are basically used with data bindings. With value converters, you can also localize data, create object based on underlying data, and even create objects and values that derive from multiple bound elements.
Probable Usages:
1. Converting a numeric code representing a job title into a string containing that job title:
- Product Owner
- Scrum Master
- Software Developer
2. Formatting Currency: Converting monetary values into currency-formatted string.
3. Formatting Dates: Formatting dates into various date formats e.g.14/04/2016, Thursday, April 14, 2016.
4. Converters to return objects: Storing references to images as string paths in database but loading and displaying images when viewing data.
In order to use Value Converters, one should first learn IValueConverter interface provided by PresentationFramework. It has two member methods:
- Convert: converts an input type to an output type. E.g. a numeric code (int type) to job title (string type).
- ConvertBack: It does the opposite of Convert.
Additionally, values converters are culture-aware. The parameters of the Convert and ConvertBack methods contain a reference to the Culture object to be used for the conversion. You can use the value of this parameter and use that information to return localized data.
How to use Value Converters:
In addition to implementing IValueConverter, you must decorate the class with the ValueConversion attribute, which specifies the source type and the target type for the converter. E.g. to convert a source type of Integer and a target type of String is shown here:
[ValueConversion(typeof(<SourceType>), typeof(<TargetType>))]
Example Code:
The example shows a TestConverter that takes int values and converts them into string.
- [ValueConversion(typeof(int), typeof(string))]
- public class TestConverter: IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
- throw new NotImplementedException();
- }
- }



Kuppurasu NagarajPosted Apr 18, 2016, 1:11 PM
Nice Sharing...
Gowtham RajamanickamPosted Apr 18, 2016, 2:10 AM
good one...
Debasis SahaPosted Apr 17, 2016, 1:17 PM
Good One..
Rahul Kumar SaxenaPosted Apr 17, 2016, 11:45 AM
Good One Shakti
Vignesh ManiPosted Apr 17, 2016, 11:06 AM
Nice