FuncConverter.cs 936 B

12345678910111213141516171819202122232425262728293031323334
  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 FuncConverter<TIn, TOut>(Func<TIn, TOut> convert, Func<TOut, TIn>? convertBack) : IValueConverter
  10. {
  11. public Func<TIn, TOut> ConvertFunc { get; set; } = convert;
  12. public Func<TOut, TIn>? ConvertBackFunc { get; set; } = convertBack;
  13. public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
  14. {
  15. if(value is TIn tIn)
  16. {
  17. return ConvertFunc(tIn);
  18. }
  19. return null;
  20. }
  21. public object? ConvertBack(object? value, Type targetType, object parameter, CultureInfo culture)
  22. {
  23. if(value is TOut tOut && ConvertBackFunc is not null)
  24. {
  25. return ConvertBackFunc.Invoke(tOut);
  26. }
  27. return null;
  28. }
  29. }