我有一个带可扩展属性的PropertyGrid。它看起来像这样:
http://i.imgur.com/2FhVV41.jpg
显示值和测量单位。现在,如果用户在根行中写入double值,则Value属性将更改,其他所有内容都保持不变。
我希望如果用户点击根行,则其文本(" 5 EUR"在图片上)将更改为仅显示值(" 5")。但是,如果用户没有点击,则文本将保持不变(" 5 EUR")。
现在我的ExpandableObjectConverter看起来像这样(只有相关部分):
ObjectProperty oldOP;
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(ObjectProperty))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
ObjectProperty op = (ObjectProperty)value;
oldOP = op;
return op.ValueProp.ToString() + " " + op.MUProp.ToString();
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
ObjectProperty op;
double newValue;
if (double.TryParse((string)value, out newValue))
{
op = oldOP;
op.ValueProp = newValue;
return op;
}
else
{
op = oldOP;
return op;
}
}