Implementing IValueConvertor has the member methods:

  • Convert: Changes the input type to an output type
  • ConvertBack: Reverses the conversion

Use the IValueConvertor to create classes that perform a variety of conversion and formatting duties.  A C# implementation would look like:

  1. [ValueConversion(typeof(int), typeof(string))]
  2.         public class myConverter : IValueConverter
  3.         {
  4.  
  5.  
  6.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  7.         {
  8.             //Place conversion code here
  9.  
  10.             //converts the object represented by the ?value? parameter into the output object,
  11.  
  12.             int Attitude = int.Parse(value.ToString());
  13.             switch (Attitude)
  14.             {
  15.                 case 1:
  16.                     return "Serious";
  17.                 case 2:
  18.                     return "You Suck";
  19.                 case 3:
  20.                     return "That's stupid";
  21.                 default:
  22.                     return "Title not defined";
  23.             }
  24.  
  25.  
  26.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27.         {
  28.             //Place conversion code here
  29.             string Attitude = value.ToString();
  30.             switch (Attitude)
  31.             {
  32.                 case "Serious":
  33.                 return 1;
  34.                 case "You Suck":
  35.                 return 2;
  36.                 case "That's stupid":
  37.                 return 3;
  38.                 default:
  39.                 return 4;
  40.             }
  41.  
  42.         }