From 6a433b8cef3d66729ae25440a0fcbd681ced9d7f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 15 Sep 2019 11:55:31 -0500 Subject: [PATCH] add PartialConverter --- .../spark/view/Converters/PartialConverter.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/client/spark/view/Converters/PartialConverter.cs diff --git a/src/client/spark/view/Converters/PartialConverter.cs b/src/client/spark/view/Converters/PartialConverter.cs new file mode 100644 index 000000000..831a42356 --- /dev/null +++ b/src/client/spark/view/Converters/PartialConverter.cs @@ -0,0 +1,35 @@ +using bts.utils; +using System; +using System.Globalization; +using System.Windows.Controls; +using System.Windows.Data; + +namespace mbts.ui.converters { + /// + /// This converter expects the first item in the values array to be either an IValueConverter, or an IMultiValueConverter, + /// then pipes the subequent value(s) into said converter's Convert method and returns the result. + /// + class PartialConverter : IMultiValueConverter { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { + switch (values.Length) { + case 0: + case 1: + break; + case 2: + if (values[0] is IValueConverter cv) + return cv.Convert(values[1], targetType, parameter, culture); + break; + default: + if (values[0] is IMultiValueConverter mvc) { + ArrayHelper.remove_at(ref values, 0); + return mvc.Convert(values, targetType, parameter, culture); + } + break; + } + return new ValidationResult(false, null); + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => + throw new NotSupportedException(); + } +} \ No newline at end of file