problem with propertygrid
-
Hi, im using propertygrid and trying to show boolean property with other string values than true/false, i made boolenconverter for this, it works fine when i use combobox to change value, but i get inner propertygrid error when i doubleclick to change value in propertygrid: Invalid property value! Details: Cannot widen from target type to primitive type.
public class BaseProps{ private bool test; [TypeConverter(typeof(BoolNameConverter2))] public bool Value { get {return test;} set {test = value;} } } public class BoolNameConverter2 : StringConverter { private const string m_trueDisplayName = "on"; private const string m_falseDisplayName = "off"; public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(bool)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (value is bool && destinationType == typeof(System.String)) { return (bool)value ? m_trueDisplayName : m_falseDisplayName; } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { return (string)value == m_trueDisplayName ? true : false; } return base.ConvertFrom(context, culture, value); } public override bool GetStandardValuesSupported( ITypeDescriptorContext context) { return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection (new string[]{m_trueDisplayName, m_falseDisplayName}); } public override bool GetStandardValuesExclusive( ITypeDescriptorContext context) { return true; } }
What could be the problem? Has anybody had similar problem and has solution? How to fix it? Thanks Deglimmer -
Hi, im using propertygrid and trying to show boolean property with other string values than true/false, i made boolenconverter for this, it works fine when i use combobox to change value, but i get inner propertygrid error when i doubleclick to change value in propertygrid: Invalid property value! Details: Cannot widen from target type to primitive type.
public class BaseProps{ private bool test; [TypeConverter(typeof(BoolNameConverter2))] public bool Value { get {return test;} set {test = value;} } } public class BoolNameConverter2 : StringConverter { private const string m_trueDisplayName = "on"; private const string m_falseDisplayName = "off"; public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(bool)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (value is bool && destinationType == typeof(System.String)) { return (bool)value ? m_trueDisplayName : m_falseDisplayName; } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { return (string)value == m_trueDisplayName ? true : false; } return base.ConvertFrom(context, culture, value); } public override bool GetStandardValuesSupported( ITypeDescriptorContext context) { return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection (new string[]{m_trueDisplayName, m_falseDisplayName}); } public override bool GetStandardValuesExclusive( ITypeDescriptorContext context) { return true; } }
What could be the problem? Has anybody had similar problem and has solution? How to fix it? Thanks DeglimmerHi, Your converter should derive from BooleanConverter. Just override the ConvertTo and ConvertFrom methods to change the display strings. Best regards, Nicolas Cadilhac @ VisualHint Smart PropertyGrid.Net Microsoft PropertyGrid Resource List Free PropertyGrid for MFC Smart FieldPackEditor.Net / DateTimePicker