![](/img/trans.png)
[英]How do I make an image property in a winforms user control available in the properties window
[英]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.