40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
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;
|
||
}
|
||
}
|
||
|