Files
WCS/Plugins/Driver/Cowain.Driver/Converters/ListToJoinedStringConverter.cs
2026-03-02 09:08:20 +08:00

40 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
/// <summary>
/// 将多选集合拼接为字符串(如:"系统, 设备"
/// </summary>
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<object> 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;
}
}