diff options
| author | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
| commit | 0440c722a221b8068bbf388c1c0c51f0faff0451 (patch) | |
| tree | 169cbf90c50ff7961db82ecb606c50c2a45a1688 /Gestor.Common/Gestor.Common.Helpers/EnumBindingSourceExtension.cs | |
| parent | 225aa1499e37faf9d38257caabbadc68d78b427e (diff) | |
| download | gestor-master.tar.gz gestor-master.zip | |
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; + } +} |