If IValueConvertor converts a single object, what the heck do you do with arrays of objects?  You use IMultiValueConvertor which can work with an object array and return a single value.

Let’s say you are trying to model the cost of a bad attitude, after all all of these happy people running around, there must be a cost to a bad attitude, right?  Well anyway, here is a way to use an array to do a calculation of the cost of a bad attitude.

This code isn’t tested, but it does represent an example of a simple calculator.


Code Snippet
  1. public class BadAttitudeConverter : IMultiValueConverter
  2.         {
  3.             public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  4.             {
  5.                 int BadAttitudeCount = (int)values[0];
  6.                 decimal BadAttitudeCost = (decimal)values[1];
  7.                 decimal TotalAttitudeCost = BadAttitudeCount * BadAttitudeCost;
  8.                 return TotalAttitudeCost.ToString("C");
  9.             }
  11.             public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  12.             {
  13.                 //ConvertBack isn't used in the Attitude costing model
  15.                 throw new NotImplementedException();
  16.             }
  17.         }