Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Windows;
- using System.Windows.Data;
- namespace Project.Common.Converters
- {
- public class QuantityToStringMultiValueConverter : IMultiValueConverter
- {
- private readonly DecimalConverter decimalConverter;
- public QuantityToStringMultiValueConverter()
- {
- decimalConverter = new DecimalConverter();
- }
- public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
- {
- if (values.Length != 3 || values.Any(v => v == DependencyProperty.UnsetValue))
- return null;
- string unit = (string)values[2];
- int decimalCount = (int) values[1];
- decimal quantity;
- if (values[0] is decimal)
- {
- quantity = (decimal) values[0];
- } else if (values[0] is double)
- {
- quantity = (decimal) (double)values[0];
- }
- else
- {
- throw new Exception("Unable to convert type");
- }
- return $"{decimalConverter.Convert(new object[] {quantity, decimalCount}, typeof(double))} {unit}";
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
Add Comment
Please, Sign In to add comment