propertygrid 中的 C# 类变量? [非设置者/获取者]
我几乎到处都找过了,但似乎找不到一个遮阳篷。 我正在为游戏构建一个关卡编辑器,我想在其中选择对象并在属性网格中显示它们的变量,以便在运行时编辑它们。
这是一个示例类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该改为创建这些成员(位置、宽度、高度)属性,并且还应该像其他属性一样将它们设置为虚拟的。这个类看起来应该是其他事物将从中继承的基类,并且您通过将这些项目作为公共成员而不是公共虚拟属性粘贴来限制它们的自定义能力。
当它是虚拟属性时,会发生两件事:
即使您这样做除了能够进行调试之外没有其他原因断点或属性中的一些诊断输出,这可能是值得的。
此外,它会立即与您提到的属性网格一起使用(我想)。
也就是说,反射应该能够为您提供公共成员,因此理论上您可以增强此 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:
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.