2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace ShrlAlgo.Toolkit.Mvvm.Converters;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Description(描述特性)的枚举转换同时也是枚举源的集合
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class EnumItemsSourceAndConverter : Collection<string>, IValueConverter
|
|
|
|
|
|
{
|
|
|
|
|
|
private IDictionary<object, object> displayNameToValueMap;
|
|
|
|
|
|
private Type type;
|
|
|
|
|
|
|
|
|
|
|
|
private IDictionary<object, object> valueToDisplayNameMap;
|
|
|
|
|
|
|
|
|
|
|
|
public Type Type
|
|
|
|
|
|
{
|
|
|
|
|
|
get => type;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!value.IsEnum)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("不是枚举类型", $"{value}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type = value;
|
|
|
|
|
|
Initialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 类属性到控件属性,源到目标
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <param name="targetType"></param>
|
|
|
|
|
|
/// <param name="parameter"></param>
|
|
|
|
|
|
/// <param name="culture"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
|
{
|
|
|
|
|
|
//if (value != null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// int n = (int)value;
|
|
|
|
|
|
// if (n == 0 || n == -1)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
return value == null
|
|
|
|
|
|
? null
|
|
|
|
|
|
: valueToDisplayNameMap.TryGetValue(value, out var value1)
|
|
|
|
|
|
? value1
|
|
|
|
|
|
: null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 目标到源
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <param name="targetType"></param>
|
|
|
|
|
|
/// <param name="parameter"></param>
|
|
|
|
|
|
/// <param name="culture"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
|
{
|
|
|
|
|
|
return value == null
|
|
|
|
|
|
? null
|
|
|
|
|
|
: displayNameToValueMap.TryGetValue(value, out var value1)
|
|
|
|
|
|
? value1
|
|
|
|
|
|
: null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static object GetDescription(FieldInfo fieldInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
var descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));
|
|
|
|
|
|
return descriptionAttribute != null ? descriptionAttribute.Description : fieldInfo.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
valueToDisplayNameMap = type.GetFields(BindingFlags.Static | BindingFlags.Public).ToDictionary(fi => fi.GetValue(null), GetDescription);
|
|
|
|
|
|
displayNameToValueMap = valueToDisplayNameMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
//当前converter的集合添加元素
|
|
|
|
|
|
foreach (string name in displayNameToValueMap.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
Add(name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|