如何获取 PropertyGrid TypeConversion 中的旧值?
我有一个属性网格输入的特殊情况,我需要在字符串格式下输入 Vector3,比方说“0, 5, 1”。我为它构建了转换器:
public class Vector3Converter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return (sourceType == typeof(string));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string[] splitted = ((string)value).Remove(" ").Split(new char[] { ',' });
return new Vector3(float.Parse(splitted[0]), float.Parse(splitted[1]), float.Parse(splitted[2]));
}
}
现在这在某种程度上可以工作,但出于另一个原因(我不会费心解释,它非常长,但有道理 - 我无法通过改变我的初始设计来避免它),我需要知道在设置新值之前该字段中的值是什么(这在某种程度上取决于它)。
我怎么能这样做呢?
I have a special case of property grid input where I would need to enter a Vector3 under a string format, let's say "0, 5, 1". I built the converter for it as such:
public class Vector3Converter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return (sourceType == typeof(string));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string[] splitted = ((string)value).Remove(" ").Split(new char[] { ',' });
return new Vector3(float.Parse(splitted[0]), float.Parse(splitted[1]), float.Parse(splitted[2]));
}
}
Now this works somehow, but for another reason (I won't bother explaining, it's incredibly long, but justified - I couldn't avoid it by changing my initial design), I need to know what value was in the field before setting the new one (which somehow depends on it).
How could I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您应该修复您的设计,这样您就不必这样做。请记住,类型转换器并非仅由属性网格使用,并且当没有“旧值”可言时,很可能需要进行转换。
这有点像黑客,但您可以将
context
转换为System.Windows.Forms.GridItem
,它表示属性网格上的一行,然后检查其Value
属性。显然,当在非属性网格上下文中调用
ConvertFrom
方法时,这将不起作用。Well, you should fix your design so you don't have to do this. Remember that type-converters aren't used exclusively by property-grids, and there's a good chance that a conversion may be required when there is no "old value" to speak of.
This is a bit of a hack, but you can cast the
context
to aSystem.Windows.Forms.GridItem
, which represents a row on a property-grid, and then inspect itsValue
property.Obviously, this isn't going to work when the
ConvertFrom
method is called in a non-property-grid context.