如何让C#的PropertyGrid的Category按照类的定义顺序进行排序?

默认情况C#会在PropertyGrid中按照Alphabetical的规则对Category进行排序

public class Person
{
    [Category("Score"), DisplayName("English")]
    public int English { get; set; } = 115;
    [Category("Score"), DisplayName("Chinese")]
    public int Chinese { get; set; } = 100;

    [Category("Score"), DisplayName("Math")]
    public int Math { get; set; } = 98;



    [Category("Info"), DisplayName("Name")]
    public string Name { get; set; } = "Jack";
    [Category("Info"), DisplayName("Sex")]
    public string Sex { get; set; } = "Male";
    [Category("Info"), DisplayName("Age")]
    public int Age { get; set; } = 15;
}

在这里插入图片描述
可以看到类中定义的Score是在Info前面的,但实际Score显示在Info之后。

可以通过以下三种方式实现按照类的定义顺序进行显示(即Score显示在Info之前):

1、在每个Category前面使用A — Z的字母或者0 — 9数字进行标识进行标识。

在这里插入图片描述

缺点:
a.分类超过26个(使用字母)或10个(使用数字),排序就会紊乱。例如1,10,11,2,3,…9 !
在这里插入图片描述
b.如果调整顺序或者中间插入新的Category需要变动其他Category名称,工作量大!

2、通过在Category前插入'\t'转义字符。
    public class Person
    {
        //排在前面的Category的'\t'格数需要比后面的多
        //转义字符控件会自动去除(如果没有自动去除,将PropertyGrid的UseCompatibleTextRendering属性设置为true

        [Category("\t\tScore"), DisplayName("English")]
        public int English { get; set; } = 115;
        [Category("\t\tScore"), DisplayName("Chinese")]
        public int Chinese { get; set; } = 100;

        [Category("\t\tScore"), DisplayName("Math")]
        public int Math { get; set; } = 98;


        [Category("\tInfo"), DisplayName("Name")]
        public string Name { get; set; } = "Jack";
        [Category("\tInfo"), DisplayName("Age")]
        public int Age { get; set; } = 15;
        [Category("\tInfo"), DisplayName("Sex")]
        public string Sex { get; set; } = "Male";
    }

在这里插入图片描述
缺点:
a.'\t’转义字符不直观,容易遗漏或者混乱。
b.如果调整顺序或者中间插入新的Category需要变动其他Category名称,工作量大!

3、通过给对应类型加注TypeConverter属性标识实现(Converter内部利用反射,将方式2的步骤自动实现)。
    //加注一行该属性即可
    [TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter))]
    //如果需要Category下的元素也按照类定义排序使用下面一行
    //[TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter)),CategoriesSortedByClassDefinitionConverter.ElementsSameOrder]
    public class Person
    {
        [Category("Score"), DisplayName("English")]
        public int English { get; set; } = 115;
        [Category("Score"), DisplayName("Chinese")]
        public int Chinese { get; set; } = 100;

        [Category("Score"), DisplayName("Math")]
        public int Math { get; set; } = 98;


        [Category("Info"), DisplayName("Name")]
        public string Name { get; set; } = "Jack";
        [Category("Info"), DisplayName("Age")]
        public int Age { get; set; } = 15;
        [Category("Info"), DisplayName("Sex")]
        public string Sex { get; set; } = "Male";
    }

CategoriesSortedByClassDefinitionConverter代码:

    /// <summary>
    ///1.设置PropertyGrid的PropertySort为Categorized或者CategorizedAlphabetical
    ///2.要排序的类加注该属性[TypeConverter(typeof(PropertiesSortedByClassDefinitionConverter))]
    ///3.如果需要Category下的元素也按照类定义排序,使用[TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter)),CategoriesSortedByClassDefinitionConverter.ElementsSameOrder]
    /// </summary>
    public class CategoriesSortedByClassDefinitionConverter : ExpandableObjectConverter
    {
        /// <summary>
        ///原本以为PropertySort属性为Categorized而不是CategorizedAlphabetical的话,Category下的属性会按照类定义排序。
        ///测试时发现好像有时候还是会乱序。
        /// </summary>
        [AttributeUsage(AttributeTargets.Class)]
        public class ElementsSameOrderAttribute : Attribute
        {
            public bool IsSameOrder { get; set; } = true;
            public ElementsSameOrderAttribute(bool isSameOrder = true)
            {
                IsSameOrder = isSameOrder;
            }
        }
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection pdcFirst = TypeDescriptor.GetProperties(value, attributes);
            List<PropertyDescriptor> pdc = new List<PropertyDescriptor>();
            for (int i = 0; i < pdcFirst.Count; i++)
            {
                pdc.Add(pdcFirst[i]);
            }

            //按照先子类后父类排序
            pdc = pdc.OrderBy(n => GetBaseTypeList(n.ComponentType).Count).ToList();

            var categoryNames = new List<string>();
            var categoryNamesOrigin = new List<string>();
            for (int i = 0; i < pdc.Count; i++)
            {
                var tempCategory = pdc[i].Category.Replace("\t", "");
                categoryNamesOrigin.Add(tempCategory);
                categoryNames.Add(tempCategory);
            }
            categoryNames = categoryNames.Distinct().ToList();
            var propertyNames = new List<string>();
            for (int i = 0; i < pdc.Count; i++)
            {
                propertyNames.Add(pdc[i].Name);
                //反射更改每个Category的名字达到效果
                SetValue(pdc[i], "category", categoryNamesOrigin[i].PadLeft(categoryNamesOrigin[i].Length + categoryNames.Count - categoryNames.IndexOf(categoryNamesOrigin[i]), '\t'));
            }

            //判断是否强制Category中的元素按照类的定义顺序进行排布
            bool bElementShouldSort = false;
            var contextAttrList = TypeDescriptor.GetAttributes(value).OfType<ElementsSameOrderAttribute>()?.ToList();
            for (int i = 0; i < contextAttrList.Count; i++)
            {
                if (contextAttrList[i] is ElementsSameOrderAttribute element)
                {
                    bElementShouldSort = element.IsSameOrder;
                }
            }

            return bElementShouldSort ? pdcFirst.Sort(propertyNames.ToArray()) : pdcFirst;
        }
        private void SetValue(object self, string fieldName, object value)
        {
            Type type = self.GetType();
            while (type != typeof(object))
            {
                var fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                var targetField = fields.Find(n => n.Name == fieldName || n.Name == $"<{fieldName}>k__BackingField");
                if (targetField != null)
                {
                    targetField.SetValue(self, value);
                    break;
                }
                type = type.BaseType;
            }
        }
        private List<Type> GetBaseTypeList(Type type)
        {
            List<Type> types = new List<Type>();
            if (type != null && type.BaseType != null)
            {
                types.Add(type.BaseType);
                types.AddRange(GetBaseTypeList(type.BaseType));
            }
            return types;
        }
    }

仅仅需要在类定义前加注一行[TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter))]即可!
在这里插入图片描述

原本以为PropertySort属性为Categorized而不是CategorizedAlphabetical的话,Category下的属性会按照类定义排序。使用中发现好像有时候还是会乱序!

如果想要Category下的属性元素也按照类定义进行排序,在类定义前使用
[TypeConverter(typeof(CategoriesSortedByClassDefinitionConverter)),CategoriesSortedByClassDefinitionConverter.ElementsSameOrder]

多的CategoriesSortedByClassDefinitionConverter.ElementsSameOrder是用来判断是否对元素进行强制排序的,不加的话和上面一样,仅仅对目录进行强制排序。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值