using ATS.Models; using System; using System.Globalization; using System.Linq; using System.Windows.Data; namespace ATS.Converters { public class FilteredParametersConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values.Length < 2 || values[0] == null || values[1] == null) return null; Type currentParamType = values[0] as Type; var allParameters = values[1] as System.Collections.IEnumerable; if (currentParamType == null || allParameters == null) return allParameters; // 过滤出类型匹配的参数 return allParameters.Cast() .Where(p => IsTypeMatch(currentParamType, p.Type)) .ToList(); } private bool IsTypeMatch(Type currentType, Type candidateType) { if (candidateType == null) return false; // 如果候选参数类型是 object,则匹配所有类型 if (candidateType == typeof(object)) return true; // 如果当前参数类型是 object,则匹配所有类型 if (currentType == typeof(object)) return true; // 如果类型完全相同,则匹配 if (candidateType == currentType) return true; // 处理数值类型的兼容性 if (IsNumericType(currentType) && IsNumericType(candidateType)) return true; return false; } private bool IsNumericType(Type type) { if (type == null) return false; switch (Type.GetTypeCode(type)) { case TypeCode.Byte: case TypeCode.SByte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: return true; default: return false; } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }