EncoderDirectionToBooleanConverter.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using SWRIS.Enums;
  2. using System;
  3. using System.Globalization;
  4. using System.Windows.Data;
  5. namespace SWRIS.Converters
  6. {
  7. public class EncoderDirectionToBooleanConverter : IValueConverter
  8. {
  9. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. if (value == null || parameter == null) { return false; }
  12. if (value is EncoderDirection valueDirection && Enum.TryParse(parameter.ToString(), true, out EncoderDirection parameterDirection))
  13. {
  14. return valueDirection == parameterDirection;
  15. }
  16. return false;
  17. }
  18. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  19. {
  20. // 空值检查:若输入或参数为 null,不更新源数据
  21. if (value == null || parameter == null)
  22. return EncoderDirection.Forward;
  23. // 仅当 value 为 true 时尝试转换
  24. if (value is bool boolValue && boolValue)
  25. {
  26. // 将参数解析为 EncoderDirection 枚举
  27. if (Enum.TryParse(parameter.ToString(), true, out EncoderDirection parameterDirection))
  28. {
  29. return parameterDirection; // 返回解析结果
  30. }
  31. }
  32. // 默认情况:不更新源数据
  33. return EncoderDirection.Forward;
  34. }
  35. }
  36. }