| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using SWRIS.Enums;
- using System;
- using System.Globalization;
- using System.Windows.Data;
- namespace SWRIS.Converters
- {
- public class EncoderDirectionToBooleanConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value == null || parameter == null) { return false; }
- if (value is EncoderDirection valueDirection && Enum.TryParse(parameter.ToString(), true, out EncoderDirection parameterDirection))
- {
- return valueDirection == parameterDirection;
- }
- return false;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- // 空值检查:若输入或参数为 null,不更新源数据
- if (value == null || parameter == null)
- return EncoderDirection.Forward;
- // 仅当 value 为 true 时尝试转换
- if (value is bool boolValue && boolValue)
- {
- // 将参数解析为 EncoderDirection 枚举
- if (Enum.TryParse(parameter.ToString(), true, out EncoderDirection parameterDirection))
- {
- return parameterDirection; // 返回解析结果
- }
- }
- // 默认情况:不更新源数据
- return EncoderDirection.Forward;
- }
- }
- }
|