PropertyGrid类别排序

前面写过一篇博客:
PropertyGrid排序http://greatverve.cnblogs.com/archive/2012/02/08/propergrid-order.html
这里没有解决类别排序的问题,所以到csdn提问了,结果半天没人回复,就忘了。
今天偶尔看到有人回复了,并且解决了类别排序的问题,真是太感谢了。现分享给大家。
PropertySorter 类请查看上一篇博客。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OrderedPropertyGrid;
using System.Reflection;

namespace WinToXml
{
    public partial class Form11 : Form
    {
        public Form11()
        {
            InitializeComponent(); propertyGrid1.SelectedObjectsChanged += new EventHandler(propertyGrid1_SelectedObjectsChanged);
            propertyGrid1.SelectedObject = new Person();
        }

        void propertyGrid1_SelectedObjectsChanged(object sender, EventArgs e)
        {
            propertyGrid1.Tag = propertyGrid1.PropertySort;
            propertyGrid1.PropertySort = PropertySort.CategorizedAlphabetical;
            propertyGrid1.Paint += new PaintEventHandler(propertyGrid1_Paint);
        }

        void propertyGrid1_Paint(object sender, PaintEventArgs e)
        {
            var categorysinfo = propertyGrid1.SelectedObject.GetType().GetField("categorys", BindingFlags.NonPublic | BindingFlags.Instance);
            if (categorysinfo != null)
            {
                var categorys = categorysinfo.GetValue(propertyGrid1.SelectedObject) as List<String>;
                propertyGrid1.CollapseAllGridItems();
                GridItemCollection currentPropEntries = propertyGrid1.GetType().GetField("currentPropEntries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(propertyGrid1) as GridItemCollection;
                var newarray = currentPropEntries.Cast<GridItem>().OrderBy((t) => categorys.IndexOf(t.Label)).ToArray();
                currentPropEntries.GetType().GetField("entries", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentPropEntries, newarray);
                propertyGrid1.ExpandAllGridItems();
                propertyGrid1.PropertySort = (PropertySort)propertyGrid1.Tag;
            }
            propertyGrid1.Paint -= new PaintEventHandler(propertyGrid1_Paint);
        }

        [TypeConverter(typeof(PropertySorter))]
        [DefaultProperty("Name")]
        public class Person
        {
            private List<string> categorys = new List<string>() { "B""A""C" };//排序
            private string _name = "Bob";
            private string _name1 = "Bob1";
            private DateTime _birthday = new DateTime(197511);

            [Category("A"), PropertyOrder(10)]
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            [Category("A"), PropertyOrder(9)]
            public string Name1
            {
                get { return _name1; }
                set { _name1 = value; }
            }

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

            [Category("C"), PropertyOrder(12)]
            public int Age
            {
                get
                {
                    TimeSpan age = DateTime.Now - _birthday;
                    return (int)age.TotalDays / 365;
                }
            }
        }
    }
}
url:http://greatverve.cnblogs.com/archive/2012/02/20/propergrid-category-order.html
参考:http://topic.csdn.net/u/20120208/13/24948256-e0f4-4e64-9c3c-095dc20f7ccf.html
posted @ 2012-02-20 09:15  大气象  阅读(5024)  评论(2编辑  收藏  举报
http://www.tianqiweiqi.com