property 自定义排序

来源:互联网 发布:移动运营商大数据 编辑:程序博客网 时间:2024/05/15 14:08

//
        // 摘要:
        //     使用该集合的默认排序(通常为字母顺序)对集合中的成员进行排序。
        public virtual PropertyDescriptorCollection Sort();
        //
        // 摘要:
        //     使用指定的 System.Collections.IComparer 对此集合中的成员排序。
        public virtual PropertyDescriptorCollection Sort(IComparer comparer);
        //
        // 摘要:
        //     对此集合中的成员排序。首先应用指定的顺序,然后应用此集合的默认排序,后者通常为字母顺序。
        public virtual PropertyDescriptorCollection Sort(string[] names);
        //
        // 摘要:
        //     对此集合中的成员排序。首先应用指定的顺序,然后使用指定的 System.Collections.IComparer 进行排序。
        public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer);

        /// <summary>
        /// 返回此类型说明符所表示的对象的已筛选属性描述符的集合。
        /// </summary>
        /// <param name="attributes">用作筛选器的属性数组。它可以是 null。</param>
        /// <returns>
        /// 一个 <see cref="T:System.ComponentModel.PropertyDescriptorCollection"></see>,包含此类型说明符所表示的对象的属性说明。默认为 <see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"></see>。
        /// </returns>
         public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            int i = 0;
            //parameterList是外面传入的待显示数据。
            PropertyDescriptor[] newProps = new PropertyDescriptor[parameterList.Count];
            foreach (ConfigParameter parameter in parameterList)
            {
                //由ConfigParameter,你自己定义的类型,转成PropertyDescriptor类型:
                newProps[i++] = new SystemOptionsPropertyDescriptor(parameter, true, attributes);
            }

            //取得了PropertyDescriptor[] newProps;现在可以对它排序,否则就按照newProps的原有顺序显示;
           
            //1.直接返回

            PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
            return tmpPDC;

            //2.默认字母顺序
            PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
            return tmpPDC.Sort();

            //3.数组的顺序:sortName就是属性的顺序,内容就是各个属性名。
            string[] sortName = new string[] { "a","b","c","d" };
            return tmpPDC.Sort(sortName);

            //4.comparer规则定义的排序方式
            ParameterListComparer myComp = new ParameterListComparer();//ParameterListComparer : IComparer
            return tmpPDC.Sort(myComp);

            //5.先数组,后comparer
            return tmpPDC.Sort(sortName,myComp);

            //而我采用的是把数据传入之前就排序完毕的方法:即调用之前,把数据parameterList排好顺序,再 1.直接返回。
        }

        //对于数组排序方法,可以声明称一个事件,由外部传入属性的实现顺序:
        public delegate string[] SortEventHandler();
        public class CustomTypeDescriptorExtend : CustomTypeDescriptor
        {
            public SortEventHandler OnSort;

            //......其它代码

            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                string[] sortName = OnSort();
                PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(typeof(CustomTypeDescriptorExtend), attributes);
                return tmpPDC.Sort(sortName);
            }
        }

        //使用时:
        public MyMethod()
        {
            CustomTypeDescriptorExtend s = new CustomTypeDescriptorExtend();
            s.OnSort += new SortEventHandler(SetSortors);
            PropertyGrid1.SelectedObject = s;
            PropertyGrid1.PropertySort = PropertySort.Categorized;
        }

        public string[] SetSortors()
        {
            return new string[] { "B", "A", "C" };
        }

原创粉丝点击