using Avalonia.Data.Converters; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Plugin.Cowain.Driver.Converters; /// /// 将多选集合拼接为字符串(如:"系统, 设备") /// public class ListToJoinedStringConverter : IValueConverter { public object Convert(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture) { // 参数:分隔符(默认逗号+空格) string separator = parameter as string ?? ", "; // 空值处理 if (value is not IEnumerable list || !list.Any()) { return "请选择"; // 占位提示 } // 拼接选中项的Name属性 return string.Join(separator, list .Where(item => item != null) .Select(item => item.GetType().GetProperty("Name")?.GetValue(item)?.ToString() ?? string.Empty) .Where(name => !string.IsNullOrEmpty(name))); } public object ConvertBack(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture) { // 无需反向转换 return Avalonia.Data.BindingOperations.DoNothing; } }