propertygrid 中的 C# 类变量? [非设置者/获取者]

发布于 2024-12-04 05:25:27 字数 764 浏览 1 评论 0原文

我几乎到处都找过了,但似乎找不到一个遮阳篷。 我正在为游戏构建一个关卡编辑器,我想在其中选择对象并在属性网格中显示它们的变量,以便在运行时编辑它们。

这是一个示例类:

public class InteractiveObject
{
    public Vector2 location = Vector2.Zero;
    public  float _width = 0;
    public  float _height = 0;


    virtual public float width
    {
        get
        {
            return _width;
        }
        set { _width = value; }
    }

    virtual public float height
    {
        get
        {
            return _height;
        }
        set { _height = value; }
    }

}

propertygrid 现在仅显示宽度和高度。 我将类绑定到 propertygrid,如下所示:

InteractiveObject obj = new InteractiveObject();
propertyGrid.SelectedObject = obj;

关于如何显示变量并将 propertygrid 中所做的更改反映回对象有什么想法吗? 谢谢。

I have looked pretty much everywhere but cannot seem to find an awnser to this.
I'm building a level editor for a game where I want to select objects and show their variables in a propertygrid to edit them during runtime.

Here's an example class:

public class InteractiveObject
{
    public Vector2 location = Vector2.Zero;
    public  float _width = 0;
    public  float _height = 0;


    virtual public float width
    {
        get
        {
            return _width;
        }
        set { _width = value; }
    }

    virtual public float height
    {
        get
        {
            return _height;
        }
        set { _height = value; }
    }

}

The propertygrid is only showing width and height right now.
I'm binding the class to the propertygrid like this:

InteractiveObject obj = new InteractiveObject();
propertyGrid.SelectedObject = obj;

Any idea's on how to show the variables and reflect the changes made in the propertrygrid back to the object?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

默嘫て 2024-12-11 05:25:27

您应该改为创建这些成员(位置、宽度、高度)属性,并且还应该像其他属性一样将它们设置为虚拟的。这个类看起来应该是其他事物将从中继承的基类,并且您通过将这些项目作为公共成员而不是公共虚拟属性粘贴来限制它们的自定义能力。

当它是虚拟属性时,会发生两件事:

  1. 所有访问都必须通过 get/set 进行,因为类成员不是公共的
  2. 访问可以由子类自定义

即使您这样做除了能够进行调试之外没有其他原因断点或属性中的一些诊断输出,这可能是值得的。

此外,它会立即与您提到的属性网格一起使用(我想)。

也就是说,反射应该能够为您提供公共成员,因此理论上您可以增强此 PropertyGrid(如果它是您的代码)或编写您自己的(如果不是)来显示属性和公共成员。

You should make these members (location, width, height) properties instead, and you should also make them virtual like the other properties. This class looks like it should be a base class that other things will inherit from, and you're limiting their ability to be customized by sticking those items in as public members rather than public virtual properties.

When it's a virtual property, two things happen:

  1. all access must come in through the get/set because the class member is not public
  2. that access can be customized by subclasses

Even if you do it for no other reason than being able to put a debug break point or some diagnostic output in the property, it's probably worth it.

Also, it will instantly work with the property grid you mention (I suppose).

That said, reflection should be able to give you the public members, so in theory you could enhance this PropertyGrid (if it's your code) or write your own (if it's not) that would display both properties and public members.

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