简体   繁体   中英

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”. So whenever the property grid calls sort to change the order of the properties, nothing happens.

Create a subclass of “ExpandableObjectConverter” that have the “GetProperties” method overridden to return an instance of “NoneSortingPropertyDescriptorCollection” with the properties in the correct order.

Use the [TypeConverterAttribute(typeof(MyExpandableObjectConverter))] to get your subclass of ExpandableObjectConverter used.

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. Possible values are as follow...

NoSort
Alphabetical
Categorized
CategorizedAlphabetical

I would suggest the NoSort as the appropriate value for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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