public static IEnumerable <EnumMemberInfo> GetEnumSource(Type enumType, bool useUnderlyingEnumValue = true, IValueConverter nameConverter = null, bool splitNames = false, EnumMembersSortMode sortMode = EnumMembersSortMode.Default, Func <string, bool, string> getKnownImageUriCallback = null, bool showImage = true, bool showName = true)
        {
            var result = enumType.GetFields(BindingFlags.Static | BindingFlags.Public)
                         .Where(field => DataAnnotationsAttributeHelper.GetAutoGenerateField(field))
                         .Select(field => {
                Enum value  = (Enum)field.GetValue(null);
                string name = GetEnumName(field, value, nameConverter, splitNames);

                var imageInfo     = GetImageInfo(MetadataHelper.GetAttribute <ImageAttribute>(field), MetadataHelper.GetAttribute <DXImageAttribute>(field), null, getKnownImageUriCallback);
                ImageSource image = (imageInfo.Item1 ?? imageInfo.Item2).With(x => (ImageSource) new ImageSourceConverter().ConvertFrom(x));

                return(new EnumMemberInfo(name, DataAnnotationsAttributeHelper.GetFieldDescription(field), useUnderlyingEnumValue ? GetUnderlyingEnumValue(value) : value, image, showImage, showName));
            });

            switch (sortMode)
            {
            case EnumMembersSortMode.DisplayName:
                result = result.OrderBy(x => x.Name);
                break;

            case EnumMembersSortMode.DisplayNameDescending:
                result = result.OrderByDescending(x => x.Name);
                break;

            case EnumMembersSortMode.DisplayNameLength:
                result = result.OrderBy(x => x.Name.Length);
                break;

            case EnumMembersSortMode.DisplayNameLengthDescending:
                result = result.OrderByDescending(x => x.Name.Length);
                break;
            }
            return(result.ToArray());
        }
Пример #2
0
        public static IEnumerable <EnumMemberInfo> GetEnumSource(Type enumType, bool useUnderlyingEnumValue = true, IValueConverter nameConverter = null,
                                                                 bool splitNames = false, EnumMembersSortMode sortMode = EnumMembersSortMode.Default, Func <string, bool, string> getKnownImageUriCallback = null,
                                                                 bool showImage  = true, bool showName = true, Func <string, ImageSource> getSvgImageSource = null, Size?imageSize = null)
        {
            if (enumType == null || !enumType.IsEnum)
            {
                return(Enumerable.Empty <EnumMemberInfo>());
            }
            Func <string, ImageSource> getImageSource = uri => (ImageSource) new ImageSourceConverter().ConvertFrom(uri);
            var result = enumType.GetFields(BindingFlags.Static | BindingFlags.Public)
                         .Where(field => DataAnnotationsAttributeHelper.GetAutoGenerateField(field))
                         .Select(field => {
                Enum value  = (Enum)field.GetValue(null);
                string name = GetEnumName(field, value, nameConverter, splitNames);

                var imageInfo               = GetImageInfo(MetadataHelper.GetAttribute <ImageAttribute>(field), MetadataHelper.GetAttribute <DXImageAttribute>(field), null, getKnownImageUriCallback);
                string imageUri             = ViewModelBase.IsInDesignMode ? null : (imageInfo.Item1 ?? imageInfo.Item2);
                Func <ImageSource> getImage = () => imageUri.With(TryGetImageSource(CanCreateSvgImageSource(getSvgImageSource, imageUri)
                        ? getSvgImageSource : getImageSource));
                return(new EnumMemberInfo(name, DataAnnotationsAttributeHelper.GetFieldDescription(field), useUnderlyingEnumValue ? GetUnderlyingEnumValue(value) : value,
                                          showImage, showName, getImage, imageSize, DataAnnotationsAttributeHelper.GetFieldOrder(field)));
            });

            switch (sortMode)
            {
            case EnumMembersSortMode.DisplayName:
                result = result.OrderBy(x => x.Name);
                break;

            case EnumMembersSortMode.DisplayNameDescending:
                result = result.OrderByDescending(x => x.Name);
                break;

            case EnumMembersSortMode.DisplayNameLength:
                result = result.OrderBy(x => x.Name.Length);
                break;

            case EnumMembersSortMode.DisplayNameLengthDescending:
                result = result.OrderByDescending(x => x.Name.Length);
                break;
            }
            return(result.OrderBy(x => (x.Order != null) ? x.Order : DefaultDisplayOrder).ToArray());
        }