C#PropertyGrid的:改变性质不工作? [英] C# PropertyGrid: Changing properties not working?

查看:148
本文介绍了C#PropertyGrid的:改变性质不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样在我的全球类的属性:

I have a property on my World class that looks like this:

    public Vec2 Gravity
    {
        get {
            Console.WriteLine("Getting gravity!");
            return GetGravity(); 
        }
        set {
            Console.WriteLine("Setting gravity!");
            SetGravity(value); 
        }
    }



获得严重性!正如所料,当的PropertyGrid 尝试读取值显示的字符串,但是当我尝试改变重力矢量并按回车键,没有任何反应。为什么不呢?

The "Getting gravity!" string is displayed as expected, when the PropertyGrid tries to read the value, but when I try to change the gravity vector and press enter, nothing happens. Why not?

我的 VEC2 类有属性:

    public float X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }

    public float Y
    {
        get
        {
            return y;
        }
        set
        {
            y = value;
        }
    }



这是对电网可见,这要归功于:

Which are visible on the grid, thanks to:

public class Vec2Converter : ExpandableObjectConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
    {
        if (destinationType == typeof(Vec2)) return true;
        else return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
    {
        if (destinationType == typeof(string) && value is Vec2)
        {
            Vec2 vec = (Vec2)value;
            return string.Format("{0}, {1}", vec.X, vec.Y);
        }
        else return base.ConvertTo(context, culture, value, destinationType);
    }
}






我只是说这两种方法的 Vec2Converter 和现在的作品:

    public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
    {
        if (sourceType == typeof(string)) return true;
        else return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
        {
            try
            {
                string strVal = value as string;
                var parts = strVal.Split(',');
                float x = float.Parse(parts[0]);
                float y = float.Parse(parts[1]);
                return new Vec2(x, y);
            }
            catch
            {
                throw new ArgumentException("Can not convert '" + (string)value + "'to type Vec2");
            }
        }
        else return base.ConvertFrom(context, culture, value);
    }

有通过改变字符串表示工作两者,和个体性质。为什么THIX修复它与各个属性??

It works both by changing the string representation, and the individual properties. Why does thix fix it for the case with individual properties??

我的认为的的<案件STRONG>答案是 VEC2 是一个结构,所以当全球返回重力载体,它按值传递,然后拷贝被改变,而不是实际的重力载体。

I think the answer is that Vec2 is a struct, so when World returns the Gravity vector, it passes it by value, and then the copy is changed, rather than the actual Gravity vector.

解决的办法,我认为要么是保持 CanConvertFrom ConvertFrom 方法地点。我假设那些让它工作,因为他们改变重力矢量的字符串表示,然后的又更新了实际的重力矢量。这,或使 VEC2 A类应该工作,但是我真的不能测试,因为现在我的应用程序是非常依赖于它是一个结构。

The solution, I think is either to keep the CanConvertFrom and ConvertFrom methods in place. I'm assuming those make it work because they modify the string representation of the gravity vector, and then that in turn updates the actual gravity vector. That, or making Vec2 a class ought to work, but I can't really test that now because my app is very dependent on it being a struct.

推荐答案

我觉得属性网格直接设置VEC2对象的属性(即 X 属性)。

I think the property grid is setting the properties directly on the Vec2 object (i.e. Xand Y properties).

这篇关于C#PropertyGrid的:改变性质不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆