using System; using System.Windows.Markup; namespace Gestor.Common.Helpers; public class EnumBindingSourceExtension : MarkupExtension { private Type _enumType; public Type EnumType { get { return _enumType; } set { if (!(value == _enumType)) { if (null != value && !(Nullable.GetUnderlyingType(value) ?? value).IsEnum) { throw new ArgumentException("Type must be for an Enum."); } _enumType = value; } } } public EnumBindingSourceExtension() { } public EnumBindingSourceExtension(Type enumType) { EnumType = enumType; } public override object ProvideValue(IServiceProvider serviceProvider) { if (null == _enumType) { throw new InvalidOperationException("The EnumType must be specified."); } Type type = Nullable.GetUnderlyingType(_enumType) ?? _enumType; Array values = Enum.GetValues(type); if (type == _enumType) { return values; } Array array = Array.CreateInstance(type, values.Length + 1); values.CopyTo(array, 1); return array; } }