如何获取 PropertyGrid TypeConversion 中的旧值?

发布于 2024-11-03 11:13:00 字数 736 浏览 0 评论 0原文

我有一个属性网格输入的特殊情况,我需要在字符串格式下输入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-11-10 11:13:01

好吧,您应该修复您的设计,这样您就不必这样做。请记住,类型转换器并非仅由属性网格使用,并且当没有“旧值”可言时,很可能需要进行转换。

这有点像黑客,但您可以将 context 转换为 System.Windows.Forms.GridItem,它表示属性网格上的一行,然后检查其 Value 属性。
显然,当在非属性网格上下文中调用 ConvertFrom 方法时,这将不起作用。

public override object ConvertFrom 
     (ITypeDescriptorContext context, CultureInfo culture, object value)
{
    var gridItem = context as GridItem;

    // If the context is indeed a property-grid item...
    if (gridItem != null)
    {
        var oldVector = (Vector3)gridItem.Value;
        ...
    }

}

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 a System.Windows.Forms.GridItem, which represents a row on a property-grid, and then inspect its Value property.
Obviously, this isn't going to work when the ConvertFrom method is called in a non-property-grid context.

public override object ConvertFrom 
     (ITypeDescriptorContext context, CultureInfo culture, object value)
{
    var gridItem = context as GridItem;

    // If the context is indeed a property-grid item...
    if (gridItem != null)
    {
        var oldVector = (Vector3)gridItem.Value;
        ...
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文