Ionite
10 months ago
2 changed files with 53 additions and 0 deletions
@ -0,0 +1,40 @@
|
||||
using System; |
||||
using System.Globalization; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using Avalonia.Data.Converters; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Converters; |
||||
|
||||
/// <summary> |
||||
/// Converts an enum value to an attribute |
||||
/// </summary> |
||||
/// <typeparam name="TAttribute">Type of attribute</typeparam> |
||||
public class EnumAttributeConverter<TAttribute>(Func<TAttribute, object?>? accessor = null) : IValueConverter |
||||
where TAttribute : Attribute |
||||
{ |
||||
/// <inheritdoc /> |
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
||||
{ |
||||
if (value is null) |
||||
return null; |
||||
|
||||
if (value is not Enum @enum) |
||||
throw new ArgumentException("Value must be an enum"); |
||||
|
||||
var field = @enum.GetType().GetField(@enum.ToString()); |
||||
if (field is null) |
||||
throw new ArgumentException("Value must be an enum"); |
||||
|
||||
if (field.GetCustomAttributes<TAttribute>().FirstOrDefault() is not { } attribute) |
||||
throw new ArgumentException($"Enum value {@enum} does not have attribute {typeof(TAttribute)}"); |
||||
|
||||
return accessor is not null ? accessor(attribute) : attribute; |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
||||
{ |
||||
throw new NotSupportedException(); |
||||
} |
||||
} |
@ -0,0 +1,13 @@
|
||||
using System.ComponentModel.DataAnnotations; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Converters; |
||||
|
||||
internal static class EnumAttributeConverters |
||||
{ |
||||
public static EnumAttributeConverter<DisplayAttribute> Display => new(); |
||||
|
||||
public static EnumAttributeConverter<DisplayAttribute> DisplayName => new(attribute => attribute.Name); |
||||
|
||||
public static EnumAttributeConverter<DisplayAttribute> DisplayDescription => |
||||
new(attribute => attribute.Description); |
||||
} |
Loading…
Reference in new issue