You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/UCDraw/SigmaDrawerWellElement/GenericListOptionsConverter.cs

106 lines
3.7 KiB
C#

1 month ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Globalization;
namespace GeoSigma.SigmaDrawerElement
{
public class GenericWellOptionsConverter<TSettings> : TypeConverter
where TSettings : class
{
// 定义一个数据获取函数(依赖注入)
private readonly Func<TSettings, string[]> _getOptions;
public GenericWellOptionsConverter(Func<TSettings, string[]> getOptions)
{
_getOptions = getOptions ?? throw new ArgumentNullException(nameof(getOptions));
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if (context?.Instance is TSettings settings)
{
try
{
var options = _getOptions(settings);
return new StandardValuesCollection(options ?? Array.Empty<string>());
}
catch
{
// 防止设计器崩溃
return new StandardValuesCollection(Array.Empty<string>());
}
}
return new StandardValuesCollection(Array.Empty<string>());
}
}
}
public abstract class GenericStringMapConverter<TSettings> : TypeConverter
where TSettings : class
{
// 映射表:内部值 → 显示值
protected abstract Dictionary<string, string> ValueMap { get; }
// 反向映射:显示值 → 内部值(自动构建)
private readonly Lazy<Dictionary<string, string>> _reverseMap;
// 获取选项的函数
protected abstract string[] GetOptions(TSettings settings);
public GenericStringMapConverter()
{
// 懒加载反向映射
_reverseMap = new Lazy<Dictionary<string, string>>(() =>
ValueMap.GroupBy(kvp => kvp.Value)
.ToDictionary(g => g.Key, g => g.First().Key, StringComparer.OrdinalIgnoreCase));
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if (context?.Instance is TSettings settings)
{
var options = GetOptions(settings);
return new StandardValuesCollection(options ?? Array.Empty<string>());
}
return new StandardValuesCollection(Array.Empty<string>());
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is string str)
{
if (ValueMap.TryGetValue(str, out var display))
return display;
return str;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string str)
{
if (_reverseMap.Value.TryGetValue(str, out var internalValue))
return internalValue;
return str;
}
return base.ConvertFrom(context, culture, value);
}
}