日期:2014-05-17  浏览次数:21320 次

怎样为PropertyGrid排序(属性类别排序,属性排序)
有个工具类可以把每个属性类别里的属性排序,但是不能把属性类别排序。
为属性类添加属性:[TypeConverter(typeof(PropertySorter))]
为每个属性添加属性:[PropertyOrder(10)]
我写了篇博客:http://www.cnblogs.com/greatverve/archive/2012/02/08/propergrid-order.html
请教:怎样把属性类别排序?谢谢。

private void Form_Load(object sender, EventArgs e)
{
    propertyGrid1.SelectedObject = new Person();
}
[TypeConverter(typeof(PropertySorter))]
[DefaultProperty("Name")]
public class Person
{
    protected const string PERSONAL_CAT = "Personal Details";
    
    private string _name = "Bob";
    private DateTime _birthday = new DateTime(1975,1,1);

    [Category(PERSONAL_CAT), PropertyOrder(10)]
    public string Name
    {
        get {return _name;} 
        set {_name = value;} 
    }

    [Category(PERSONAL_CAT), PropertyOrder(11)]
    public DateTime Birthday
    {
        get {return _birthday;}
        set {_birthday = value;}
    }

    [Category(PERSONAL_CAT), PropertyOrder(12)]
    public int Age
    {
        get 
        {
            TimeSpan age = DateTime.Now - _birthday; 
            return (int)age.TotalDays / 365; 
        }
    }
}

工具类:

//
// (C) Paul Tingey 2004 
//
using System;
using System.Collections;
using System.ComponentModel;

namespace OrderedPropertyGrid
{
    public class PropertySorter : ExpandableObjectConverter
    {
        #region Methods
        public override bool GetPropertiesSupported(ITypeDescriptorContext context) 
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            //
            // This override returns a list of properties in order
            //
            PropertyDescriptorColl