更新
This commit is contained in:
28
ShrlAlgoToolkit.Mvvm/Converters/EnumDescriptionExtension.cs
Normal file
28
ShrlAlgoToolkit.Mvvm/Converters/EnumDescriptionExtension.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user