我不喜歡這樣的解決方案,但它似乎工作:
與所有的「排序」創建一個子類「PropertyDescriptorCollection」的方法重載只是回到「本」。所以每當屬性網格調用排序來改變屬性的順序時,什麼都不會發生。
創建一個「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}
}
這將意味着壓倒一切的行爲也一樣,如果你可以從該屬性繼承。 – leppie 2011-01-07 10:08:39