Мне не нравится это решение, но, похоже, оно работает:
Создайте подкласс «PropertyDescriptorCollection» со всеми переопределенными методами «Sort», чтобы просто вернуть «this».Таким образом, всякий раз, когда сетка свойств вызывает сортировку для изменения порядка свойств, ничего не происходит.
Создайте подкласс «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}
}