简体   繁体   English

如何控制winforms属性网格中ExpandableObject属性的顺序?

[英]How do I control the order of ExpandableObject properties in the winforms property grid?

I have classes like: 我有类似的课程:

[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();

I wish the properties of “inner” to be displayed as “Before”, “After”, the property grid seems to put them in alphabetically order and hence display them as “After”, “Before” 我希望“内部”的属性显示为“之前”,“之后”,属性网格似乎按字母顺序排列它们,因此将它们显示为“之后”,“之前”

I do not like this solution but it seems to work: 我不喜欢这种解决方案,但它似乎有效:

Create a sub class of “PropertyDescriptorCollection” with all “Sort” methods override just to return “this”. 创建一个“ PropertyDescriptorCollection”的子类,并覆盖所有“ Sort”方法,仅返回“ this”。 So whenever the property grid calls sort to change the order of the properties, nothing happens. 因此,每当属性网格调用sort更改属性顺序时,都不会发生任何事情。

Create a subclass of “ExpandableObjectConverter” that have the “GetProperties” method overridden to return an instance of “NoneSortingPropertyDescriptorCollection” with the properties in the correct order. 创建一个“ ExpandableObjectConverter”的子类,该子类具有覆盖“ GetProperties”方法的属性,以返回具有正确顺序属性的“ NoneSortingPropertyDescriptorCollection”的实例。

Use the [TypeConverterAttribute(typeof(MyExpandableObjectConverter))] to get your subclass of ExpandableObjectConverter used. 使用[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}      
}    

I know this an old question but this solution is simpler than the accepted one: 我知道这是一个老问题,但是此解决方案比公认的解决方案更简单:

public class MyExpandableObjectConverter : ExpandableObjectConverter
{
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        return TypeDescriptor.GetProperties(typeof(Inner), attributes).Sort(new[] { "Before", "After" });
    }
}

[TypeConverterAttribute(typeof(MyExpandableObjectConverter))]
public class Inner
{
    public string Before { get; set; }
    public string After { get; set; }
}

Use the PropertyGrid.PropertySort property to alter the ordering of the properties. 使用PropertyGrid.PropertySort属性来更改属性的顺序。 Possible values are as follow... 可能的值如下...

NoSort
Alphabetical
Categorized
CategorizedAlphabetical

I would suggest the NoSort as the appropriate value for you. 我建议将NoSort作为适合您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM