我有这样的类(class):
[TypeConverterAttribute(typeof(ExpandableObjectConverter))]
public class Inner
{
public string Before{get;set}
public string After(get;set}
}
public class Outer
{
public Inner Inner {get;set}
}
myPropertygrid.SelectedObject = new Outer();
我希望“inner”的属性显示为“Before”,“After”,属性网格似乎将它们按字母顺序排列,因此显示为“After”,“Before”
最佳答案
我不喜欢这个解决方案,但它似乎有效:
创建“PropertyDescriptorCollection”的子类,并覆盖所有“Sort”方法以返回“this”。因此,每当属性网格调用 sort 来更改属性的顺序时,都不会发生任何事情。
创建“ExpandableObjectConverter”的子类,重写“GetProperties”方法以返回“NoneSortingPropertyDescriptorCollection”的实例,其中属性的顺序正确。
使用 [TypeConverterAttribute(typeof(MyExpandableObjectConverter))] 来使用 ExpandableObjectConverter 的子类。
public class NoneSortingPropertyDescriptorCollection : PropertyDescriptorCollection
{
public NoneSortingPropertyDescriptorCollection(PropertyDescriptor[] propertyDescriptors)
: base(propertyDescriptors)
{
}
public override PropertyDescriptorCollection Sort()
{
return this;
}
public override PropertyDescriptorCollection Sort(string[] names)
{
return this;
}
public override PropertyDescriptorCollection Sort(string[] names, System.Collections.IComparer comparer)
{
return this;
}
public override PropertyDescriptorCollection Sort(System.Collections.IComparer comparer)
{
return this;
}
}
public class MyExpandableObjectConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection d = base.GetProperties(context, value, attributes);
List<PropertyDescriptor> props = new List<PropertyDescriptor>();
props.Add(d.Find("Before", false));
props.Add(d.Find("After", false));
NoneSortingPropertyDescriptorCollection m = new NoneSortingPropertyDescriptorCollection(props.ToArray());
return m;
}
}
[TypeConverterAttribute(typeof(MyExpandableObjectConverter))]
public class Inner
{
public string Before{get;set}
public string After(get;set}
}
关于.net - 如何控制 winforms 属性网格中 ExpandableObject 属性的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4624111/