HashCalculator.GUI/Converters/MultiValueAggregateConverter.cs
2022-01-25 19:05:37 +01:00

46 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
namespace HashCalculator.GUI.Converters
{
class MultiValueAggregateConverter : List<IValueConverter?>, IMultiValueConverter
{
public IMultiValueConverter? Converter { get; set; }
public IValueConverter? PostProcess { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (Converter is not { } converter)
{
throw new InvalidOperationException($"{nameof(Converter)} of {nameof(MultiValueAggregateConverter)} is null");
}
if (Count > 0)
{
values = values.ToArray(); // 复制一份
var length = Math.Min(Count, values.Length);
for (var i = 0; i < length; ++i)
{
if (this[i] is IValueConverter c)
{
values[i] = c.Convert(values[i], targetType, parameter, culture);
}
}
}
var result = converter.Convert(values, targetType, parameter, culture);
if (PostProcess is not { } postConverter)
{
return result;
}
return postConverter.Convert(result, targetType, parameter, culture);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}