Guest User

Untitled

a guest
Jan 17th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Data;
  6.  
  7. namespace Project.Common.Converters
  8. {
  9.     public class QuantityToStringMultiValueConverter : IMultiValueConverter
  10.     {
  11.         private readonly DecimalConverter decimalConverter;
  12.  
  13.         public  QuantityToStringMultiValueConverter()
  14.         {
  15.             decimalConverter = new DecimalConverter();
  16.         }
  17.  
  18.         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  19.         {
  20.             if (values.Length != 3 || values.Any(v => v == DependencyProperty.UnsetValue))
  21.                 return null;
  22.  
  23.             string unit = (string)values[2];
  24.             int decimalCount = (int) values[1];
  25.  
  26.             decimal quantity;
  27.             if (values[0] is decimal)
  28.             {
  29.                 quantity = (decimal) values[0];
  30.             } else if (values[0] is double)
  31.             {
  32.                 quantity = (decimal) (double)values[0];
  33.             }
  34.             else
  35.             {
  36.                 throw new Exception("Unable to convert type");
  37.             }
  38.  
  39.             return $"{decimalConverter.Convert(new object[] {quantity, decimalCount}, typeof(double))} {unit}";
  40.         }
  41.  
  42.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  43.         {
  44.             throw new NotImplementedException();
  45.         }
  46.     }
  47. }
Add Comment
Please, Sign In to add comment