簡體   English   中英

如何控制winforms屬性網格中ExpandableObject屬性的順序?

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

我有類似的課程:

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

我希望“內部”的屬性顯示為“之前”,“之后”,屬性網格似乎按字母順序排列它們,因此將它們顯示為“之后”,“之前”

我不喜歡這種解決方案,但它似乎有效:

創建一個“ 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}      
}    

我知道這是一個老問題,但是此解決方案比公認的解決方案更簡單:

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; }
}

使用PropertyGrid.PropertySort屬性來更改屬性的順序。 可能的值如下...

NoSort
Alphabetical
Categorized
CategorizedAlphabetical

我建議將NoSort作為適合您的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM