我正在将Collection显示为可扩展对象。 我只能按字母顺序排序,或者根本不排序。
按字母顺序排除:
No Sort结果如下:
我试图找到一种按索引对集合进行排序的方法,因此它看起来像
[0], [1], [2], [3], ..., [9], [10], [11], [12], [13], [14].
我无法找到一种可靠的方法来做到这一点。
我确实找到了解决方案here,但他们使用的ListItemDescriptor
似乎不存在。
这是我的customteepeconverter,我如何在propertygrid中的对象和customditor中拥有该集合。
自定义编辑器只强制它可扩展,而不是让用户选择打开默认编辑器并添加更多项目。
这是针对UserData而不是MarkData,我已经完成了针对UserData的所有工作,但是我将这些图像用于MarkData,因为它是一个较小的集合,用于更容易的图像。
internal class UserDataCollectionConverter : ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
{
if (destType == typeof(string) && value is UserDataCollection)
{
return "User Data";
}
return base.ConvertTo(context, culture, value, destType);
}
}
对象
[EditorAttribute(typeof(UserDataEditor), typeof(UITypeEditor))]
[DescriptionAttribute("Custom user data."),
TypeConverter(typeof(UserDataCollectionConverter)),
CategoryAttribute("Custom")]
public UserDataCollection UserData
{
get { return _userData; }
set { _userData = value; }
}
自定义编辑器
class UserDataEditor : System.ComponentModel.Design.CollectionEditor
{
public UserDataEditor(Type type)
: base(type)
{
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
UserDataCollection things = new UserDataCollection();
return value;
}
}
UserDataCollection
using System;
using System.Collections;
using System.ComponentModel;
namespace CMS.LaserGraf.Data
{
public class UserDataCollection : CollectionBase, ICustomTypeDescriptor
{
#region
public void Add(string value)
{
this.List.Add(value);
}
public void Add(int number)
{
for (int i = 0; i < number; i++)
{
this.List.Add("");
}
}
public void Remove(string value)
{
this.List.Remove(value);
}
public void set(string[] values)
{
this.List.Clear();
foreach (string s in values)
{
this.List.Add(s);
}
}
public string[] getUserDataArray()
{
int i = 0;
string[] retval = new string[this.List.Count];
foreach (string s in this.List)
{
retval[i++] = s;
}
return retval;
}
public string this[int index]
{
get
{
return (string)this.List[index];
}
set
{
this.List[index] = value;
}
}
#endregion
// Implementation of interface ICustomTypeDescriptor
#region ICustomTypeDescriptor impl
public String GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public String GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public PropertyDescriptorCollection GetProperties()
{
// Create a collection object to hold property descriptors
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
// Iterate the list of ProcessCounters
for (int i = 0; i < this.List.Count; i++)
{
// Create a property descriptor for the ProcessCounter item and add to the property descriptor collection
UserDataCollectionPropertyDescriptor pd = new UserDataCollectionPropertyDescriptor(this, i);
pds.Add(pd);
}
// return the property descriptor collection
return pds;
}
#endregion
}
}
答案 0 :(得分:0)
您似乎已经按照以下代码中的特定顺序添加
// Iterate the list of ProcessCounters
for (int i = 0; i < this.List.Count; i++)
{
// Create a property descriptor for the ProcessCounter item and add to the property descriptor collection
UserDataCollectionPropertyDescriptor pd = new UserDataCollectionPropertyDescriptor(this, i);
pds.Add(pd);
}
您可以通过两种方式实现排序。
UserDataCollectionPropertyDescriptor
方法中添加PropertyDescriptorCollection
时对GetProperties()
进行排序。PropertyDescriptorCollection
类并覆盖Sort(IComparer comparer)
方法并传入自定义比较器以进行比较UserDataCollectionPropertyDescriptor
。示例代码
public class UserDataPropertyDescriptorCollection : PropertyDescriptorCollection
{
public override PropertyDescriptorCollection Sort(IComparer comparer)
{
UserDataComparer customComparer = new UserDataComparer();
return base.Sort(customComparer);
}
}
public class UserDataComparer : IComparer
{
#region IComparer Members
int IComparer.Compare(object x, object y)
{
//cast and compare UserDataCollectionPropertyDescriptor index values
}
#endregion
}