40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Windows.Markup;
|
|
|
|
namespace MainShell.Converter
|
|
{
|
|
// XAML 中可直接写 {conv:EnumBindingSource {x:Type local:MyEnum}}
|
|
public class EnumBindingSourceExtension : MarkupExtension
|
|
{
|
|
public Type EnumType { get; set; }
|
|
public bool UseDescription { get; set; } = true;
|
|
|
|
public EnumBindingSourceExtension() { }
|
|
|
|
public EnumBindingSourceExtension(Type enumType)
|
|
{
|
|
EnumType = enumType;
|
|
}
|
|
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
{
|
|
if (EnumType == null || !EnumType.IsEnum) return null;
|
|
|
|
var values = Enum.GetValues(EnumType).Cast<Enum>();
|
|
return values.Select(v => new EnumItem
|
|
{
|
|
Value = v,
|
|
Description = UseDescription ? EnumHelper.GetEnumDescription(v) : v.ToString()
|
|
}).ToList();
|
|
}
|
|
}
|
|
|
|
public class EnumItem
|
|
{
|
|
public Enum Value { get; set; }
|
|
public string Description { get; set; }
|
|
public int IntValue => Convert.ToInt32(Value);
|
|
}
|
|
}
|