
Hi,
I have a class like this:
class Foo
{
[Category("Foo")]
public int X;
//...
[Category("Test", TypeConverter(typeof(MyCollectionConverter))]
public Dictionary<int,string> MyDic = new Dictionary<int,string>()
}
and a typeconverter which shows the integer key as hex decimal string:
sealed class MyCollectionConverter : ExpandableObjectConverter {
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
var properties = new ArrayList();
var dictionary = (Dictionary<int, int>)value;
foreach (var e in dictionary) {
properties.Add(new DictionaryPropertyDescriptor(dictionary, e.Key));
}
var props = (PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(props);
}
class DictionaryPropertyDescriptor : PropertyDescriptor {
readonly IDictionary _dictionary;
public readonly int _key;
internal DictionaryPropertyDescriptor(IDictionary d, int key)
: base(key.ToString(), null) {
_dictionary = d;
_key = key;
}
public override Type PropertyType => _dictionary[_key].GetType();
public override void SetValue(object component, object value) {
}
public override string Description => _key.ToString("X");
public override object GetValue(object component) {
return ((int)_dictionary[_key]).ToString("X");
}
public override bool IsReadOnly => true;
public override Type ComponentType => null;
public override bool CanResetValue(object component) => false;
public override void ResetValue(object component) {
}
public override bool ShouldSerializeValue(object component) => false;
}
}
My problem is that in propertygrid the dictionary is alphabetically sorted because the description is a hex decimal string which results in (0x)100 is before (0x)8. But I would like to sort it by integer or by the order in dictionary so (0x)8 is before (0x)100 as 8 (int) is smaller than 64 (int).
But I also don't want to change the overall order of the propertygrid, only the dictionary values need to be sorted. Is there a way to achieve this?
//edit: I've seen the Sort category in the demo application but is there a way to do this via converter / reflection attribute instead?
[Modified 6 years ago]