Files
Shrlalgo.RvKits/ShrlAlgoToolkit.Mvvm/Converters/EnumDescriptionExtension.cs

29 lines
839 B
C#
Raw Normal View History

2025-07-11 09:20:23 +08:00
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Markup;
namespace ShrlAlgoToolkit.Mvvm.Converters
{
public class EnumDescriptionExtension : MarkupExtension
{
private readonly object _enumValue;
public EnumDescriptionExtension(object enumValue)
{
_enumValue = enumValue;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (_enumValue == null) return string.Empty;
var field = _enumValue.GetType().GetField(_enumValue.ToString());
var attribute = field?.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;
return attribute?.Description ?? _enumValue.ToString();
}
}
}