IValueConvertor versus IMultiValueConvertor, nothing free here
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
- public class BadAttitudeConverter : IMultiValueConverter
- {
- public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- int BadAttitudeCount = (int)values[0];
- decimal BadAttitudeCost = (decimal)values[1];
- decimal TotalAttitudeCost = BadAttitudeCount * BadAttitudeCost;
- return TotalAttitudeCost.ToString("C");
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
- {
- //ConvertBack isn't used in the Attitude costing model
- throw new NotImplementedException();
- }
- }