using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace Avalonia.Data.Converters { /// /// A general purpose that uses a /// to provide the converter logic. /// /// The type of the inputs. /// The output type. public class FuncMultiValueConverter : IMultiValueConverter { private readonly Func, TOut> _convert; /// /// Initializes a new instance of the class. /// /// The convert function. public FuncMultiValueConverter(Func, TOut> convert) { _convert = convert; } /// public object? Convert(IList values, Type targetType, object? parameter, CultureInfo culture) { //standard OfType skip null values, even they are valid for the Type static IEnumerable OfTypeWithDefaultSupport(IList list) { foreach (var obj in list) { if (obj is TIn result) { yield return result; } else if (Equals(obj, default(TIn))) { yield return default; } } } var converted = OfTypeWithDefaultSupport(values).ToList(); if (converted.Count == values.Count) { return _convert(converted); } else { return AvaloniaProperty.UnsetValue; } } } }