Reputation: 212
I'm trying to edit complex objects in a PropertyGrid control. I add the ExpandableObjectConverter (or my own subclass when I need it) as the TypeConverter and it works fine.
The one thing I can't seem to figure out is this. The object itself will have its .ToString() representation next to it in the Grid. Then when I expand the object the attributes have the same. All can be editable. I want to disable editing of the ToString() object field, but keep the attributes editable.
So in the PropertyGrid it would look like this;
+ Color {(R,G,B,A) = (255,255,255,255)} --uneditable
Alpha 255 --editable
Blue 255 --editable
Green 255 --editable
Red 255 --editable
So far I haven't found a way to do this. If I try to make it ReadOnly the entire object becomes read only. If I specify my own ExpandableObjectConverter and state it cannot convert from a string, if the string is edited in the PropertyGrid it will still try to cast and then fail.
I essentially just want it so I can stop end users from editing the string and forcing them to edit the individual attributes instead, so that I don't have to write a string parser for every single class.
Is this possible, or is there another way of doing this I just haven't thought of?
Upvotes: 4
Views: 971
Reputation: 1064004
This seems to do the trick:
[TypeConverter(typeof (Color.ColorConverter))]
public struct Color
{
private readonly byte alpha, red, green, blue;
public Color(byte alpha, byte red, byte green, byte blue)
{
this.alpha = alpha;
this.red = red;
this.green = green;
this.blue = blue;
}
public byte Alpha { get { return alpha; } }
public byte Red { get { return red; } }
public byte Green { get { return green; } }
public byte Blue { get { return blue; } }
public override string ToString()
{
return string.Format("{{(R,G,B,A) = ({0},{1},{2},{3})}}", Red, Green, Blue, Alpha);
}
private class ColorConverter : ExpandableObjectConverter
{
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
return new Color((byte)propertyValues["Alpha"], (byte)propertyValues["Red"],
(byte) propertyValues["Green"], (byte) propertyValues["Blue"]);
}
}
}
Upvotes: 5