PropertyGrid類別排序

來源:互聯網
上載者:User
前面寫過一篇部落格:
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(1975, 1, 1);

            [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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.