From f6f167221d08dcb08a076da7f609cf982b545a60 Mon Sep 17 00:00:00 2001 From: chenjie02_cd <836904362@qq.com> Date: Mon, 6 Sep 2021 20:10:07 +0800 Subject: [PATCH] EnumDataProvider support for Attributes --- .../Data/Enum/EnumDataProvider.cs | 61 ++++++++++++++++++- .../HandyControl_Shared/Data/EnumItem.cs | 9 +++ .../HandyControl_Shared.projitems | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/Shared/HandyControl_Shared/Data/EnumItem.cs diff --git a/src/Shared/HandyControl_Shared/Data/Enum/EnumDataProvider.cs b/src/Shared/HandyControl_Shared/Data/Enum/EnumDataProvider.cs index 132c5a43..304860af 100644 --- a/src/Shared/HandyControl_Shared/Data/Enum/EnumDataProvider.cs +++ b/src/Shared/HandyControl_Shared/Data/Enum/EnumDataProvider.cs @@ -1,10 +1,18 @@ using System; +using System.Collections.Generic; +using System.ComponentModel; using System.Windows.Data; namespace HandyControl.Data { public class EnumDataProvider : ObjectDataProvider { + public EnumDataProvider() + { + MethodName = nameof(GetValues); + ObjectType = typeof(System.Enum); + } + private Type _type; public Type Type { @@ -12,10 +20,59 @@ namespace HandyControl.Data set { _type = value; - MethodName = "GetValues"; - ObjectType = typeof(System.Enum); MethodParameters.Add(value); } } + + private bool _useAttributes; + + public bool UseAttributes + { + get => _useAttributes; + set + { + _useAttributes = value; + ObjectType = value ? typeof(EnumDataProvider) : typeof(System.Enum); + } + } + + public static IEnumerable GetValues(Type enumType) + { + if (enumType == null) + { + throw new ArgumentNullException(nameof(enumType)); + } + + var resultList = new List(); + + var values = System.Enum.GetValues(enumType); + foreach (System.Enum value in values) + { + var fieldInfo = enumType.GetField(value.ToString()); + if (fieldInfo == null) + { + continue; + } + + if (fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), true) is BrowsableAttribute[] { Length: > 0 } browsableAttributes) + { + if (!browsableAttributes[0].Browsable) + { + continue; + } + } + + if (fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true) is DescriptionAttribute[] { Length: > 0 } descriptionAttributes) + { + resultList.Add(new EnumItem + { + Description = descriptionAttributes[0].Description, + Value = value + }); + } + } + + return resultList; + } } } diff --git a/src/Shared/HandyControl_Shared/Data/EnumItem.cs b/src/Shared/HandyControl_Shared/Data/EnumItem.cs new file mode 100644 index 00000000..191115e4 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Data/EnumItem.cs @@ -0,0 +1,9 @@ +namespace HandyControl.Data +{ + public class EnumItem + { + public string Description { get; set; } + + public System.Enum Value { get; set; } + } +} diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index ce088db4..da30695d 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -83,6 +83,7 @@ +