MultiFuncConverter.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. namespace InABox.Wpf;
  9. public class MultiFuncConverter(Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null, IValueConverter? wrappedConverter = null) : IMultiValueConverter
  10. {
  11. public Func<object?[], object?> ConvertFunc { get; set; } = convert;
  12. public Func<object?, object?[]?>? ConvertBackFunc { get; set; } = convertBack;
  13. public IValueConverter? wrappedConverter = wrappedConverter;
  14. public object? Convert(object?[] values, Type targetType, object parameter, CultureInfo culture)
  15. {
  16. var val = ConvertFunc(values);
  17. if(wrappedConverter is not null)
  18. {
  19. return wrappedConverter.Convert(val, targetType, parameter, culture);
  20. }
  21. else
  22. {
  23. return val;
  24. }
  25. }
  26. public object?[]? ConvertBack(object? value, Type[] targetTypes, object parameter, CultureInfo culture)
  27. {
  28. if(wrappedConverter is not null)
  29. {
  30. value = wrappedConverter.Convert(value, typeof(object), parameter, culture);
  31. }
  32. if(ConvertBackFunc is not null)
  33. {
  34. return ConvertBackFunc(value);
  35. }
  36. return null;
  37. }
  38. }