diff options
Diffstat (limited to 'Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs')
| -rw-r--r-- | Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs b/Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs new file mode 100644 index 0000000..67981a6 --- /dev/null +++ b/Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs @@ -0,0 +1,54 @@ +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; + } +} |