解决PropertyGrid对自定义属性排序的问题.

参考了:http://www.cnblogs.com/greatverve/archive/2012/02/08/propergrid-order.html

引用命名空间:   using System.ComponentModel;

主要代码如下:

/**
* 用于属性排序
* **/
namespace CustomControls.DrawItem
{
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
//
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
ArrayList orderedProperties = new ArrayList();
foreach (PropertyDescriptor pd in pdc)
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
orderedProperties.Add(new PropertyOrderPair(pd.Name, ));
}
}
//
// Perform the actual order using the value PropertyOrderPair classes
// implementation of IComparable to sort
//
orderedProperties.Sort();
//
// Build a string list of the ordered names
//
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
propertyNames.Add(pop.Name);
}
//
// Pass in the ordered list for the PropertyDescriptorCollection to sort by
//
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
}
#endregion
} #region Helper Class - PropertyOrderAttribute
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
//
// Simple attribute to allow the order of a property to be specified
//
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
} public int Order
{
get
{
return _order;
}
}
}
#endregion #region Helper Class - PropertyOrderPair
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
public string Name
{
get
{
return _name;
}
} public PropertyOrderPair(string name, int order)
{
_order = order;
_name = name;
} public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name, otherName);
}
else if (otherOrder > _order)
{
return -;
}
return ;
}
}
#endregion }

使用方法:

一、在属性类中,先做如下添加:

    [TypeConverter(typeof(PropertySorter))]
[DefaultProperty("DrawLine")]
public class DrawLine
{
.......
}

二、在属性类中,对每个属性名称,做如下说明:

       private Point pfs = new Point();
/// <summary>
/// 直线的开始坐标
/// </summary>
[CategoryAttribute("坐标"), Description("开始坐标"), DisplayName("开始坐标"), PropertyOrder()] public Point StartPoint
{
get
{
return pfs;
}
set
{
pfs = value;
}
}

如上述操作,即可完成,谢谢!

PropertyGrid排序的更多相关文章

  1. PropertyGrid—属性类别排序

    属性默认按照字母顺序排序,有时,我们想要按自定义的顺序排序 这个工具类可以把每个属性类别里的属性排序,但是不能把属性类别排序. 为属性类添加属性:[TypeConverter(typeof(Prope ...

  2. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  3. easyui propertygrid 动态绑定

    从$.fn.datagrid.defaults继承,覆盖默认值 $.fn.propertygrid.defaults propertygrid 提供用户一个接口,浏览和编辑对象属性,propertyg ...

  4. C# PropertyGrid控件应用心得

    何处使用 PropertyGrid 控件 在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验.例如,某个应用程序包含多个用户可以设置的“设置”或选项 ...

  5. PropertyGrid—添加属性Tab

    零.引言 PropertyGrid用来显示和编辑对象的属性,前面已经简单介绍了如何使用该控件和提供不同的属性编辑方法.前面主要讲如何使用该控件,但有时,该控件无法满足我们的需求,就需要对其进行扩展.本 ...

  6. 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格

    EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...

  7. 关于PropertyGrid控件的排序问题

    前些天,由于在项目中需要用到PropertyGrid这个控件,展现其所在控件的某些属性,由于有些控件的属性较多,不易浏览,而且PropertyGrid的排序默认的按照字母的顺序排列的,这样导致在在某些 ...

  8. C# PropertyGrid控件应用心得 【转】

    源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...

  9. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

随机推荐

  1. UVA 12901 Refraction 几何/大雾题

    Refraction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  2. 【ArcGIS 10.2新特性】Portal for ArcGIS新特性

    1.概述 经过各版本的积累和更新,Portal for ArcGIS在ArcGIS10.2中以正式产品的形态加入到了ArcGIS系列产品线中.它有3个主要定位:协同管理平台.在线制图平台以及内容管理平 ...

  3. Swift2.0 中的String(三):类型转换

    本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...

  4. 果然还是SB了

    编译原理的龙书和虎书,各看了两章之后,¥……&……*……@%¥ 好吧,既然是码农,就要从基层做起,我尝试handwritten一下C或者C的极小子集的one-pass编译器先,等有了深切的体会 ...

  5. IPC——匿名管道

    Linux进程间通信——使用匿名管道 在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它 ...

  6. IIS 发布网站遇到的问题

    1.解决win7 64位[ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 http://wwwu8.wap.blog.163.com/w2/blogDetail.do?hostID ...

  7. 实例源码--Android图片滚动切换效果

    下载源码 技术要点:  1.图片滚动切换技术 2.详细的源码注释 ...... 详细介绍: 1.图片滚动切换技术 本套源码实现了类似于网站图片滚动推广效果,效果不错,很不错的参考源码 2.源码目录 运 ...

  8. Golang学习 - unicode/utf16 包

    ------------------------------------------------------------ // IsSurrogate 判断 r 是否为代理区字符 // 两个代理区字符 ...

  9. 学java入门到精通,不得不看的15本书

    学java入门到精通,不得不看的15本书 一.Java编程入门类1.<Java编程思想>2.<Agile Java>中文版 二.Java编程进阶类1.<重构 改善既有代码 ...

  10. MAMP Pro3.5注册码

    MAMP这个就不用介绍了,堪称MAC下的苏菲玛索,官方下载地址:https://www.mamp.info/en/mamp-pro/   ,400多大洋,土豪朋友请直接购买吧,正版还是要支持的. 和我 ...