propertyGrid自定义排序以及[...]按钮的实现等

1.现在propertyGrid显示的属性是以汉语拼音排序.怎么样实现自动排序.
2.怎么实现[...]按钮的弹出.
3.用+号显示某个属性时,怎么让他默认+号展开
1.我的意思是说,让他不排序即可,就是按属性定义的先后..或者能自定义一下也行....例如:长、宽、高、名字。。现在显示:长、高、宽、名字。
我看到过1楼这段代码,代不知道怎么用.

第1个回答  2008-03-17
我明白你的第一个问题,但是后两个不好意思没明白
第一个问题是这样
//
// 摘要:
// 使用该集合的默认排序(通常为字母顺序)对集合中的成员进行排序。
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" };
}
当然这也不是我写的,给你个原始链接吧,呵呵
http://hi.baidu.com/yangyuhang/blog/item/b2c8e3d3f88eb0dba9ec9add.html