Winforms PropertyGrid - 属性不可编辑
您好,这是我关于堆栈溢出的第一个问题,所以如果我做了任何愚蠢的事情,请原谅我。 好吧,我的问题是我正在使用关卡编辑器,我想使用 PropertyGrid 控件来编辑图块/实体等的属性。所以到目前为止一切正常,值显示正确,通过代码更改时更新,但是问题我的经验是,除非它是布尔值,否则我无法更改值,我用谷歌搜索了很多,但我根本找不到解决方案。
这是我定义属性的代码:
[Description("Defines the Position on the screen")]
public Vector2 screenpos { get; set; }
Vector2 WorldPos;
[Description("Defines the texture of the selected tile")]
public string texture { get; set; }
[Description("Defines if the player can collide with this tile")]
public bool IsCollidable { get; set; }
[Description("Defines on what layer this tile is drawn (1-3)")]
public int Layer { get; set; }
[Description("Shows if the tile is currently visible on the screen")]
public bool OnScreen { get; private set; }
我可以编辑 IsCollidable,如果我从 OnScreen 的集合中删除私有,我也可以编辑它,但我不能编辑其他任何内容,哦,如果您能稍微说明一下您的答案,我将不胜感激更简单,我不是一个经验丰富的程序员,提前谢谢。
Hello this is my first question of stack overflow, so forgive me if i do anything stupid.
Well my problem is that I'm working on a level editor and i want to use a PropertyGrid control to edit properties of tiles/entities etc.. so everything works so far, the values show correctly, update when changed trough code but the problem i am expierencing is that I cannot change values unless it is a boolean, i googled alot but i simply could find no solutions.
Here is the code where i define the properties:
[Description("Defines the Position on the screen")]
public Vector2 screenpos { get; set; }
Vector2 WorldPos;
[Description("Defines the texture of the selected tile")]
public string texture { get; set; }
[Description("Defines if the player can collide with this tile")]
public bool IsCollidable { get; set; }
[Description("Defines on what layer this tile is drawn (1-3)")]
public int Layer { get; set; }
[Description("Shows if the tile is currently visible on the screen")]
public bool OnScreen { get; private set; }
I can edit the IsCollidable and if i remove the private from OnScreen's set i can edit that too but i can not edit anything else, oh and i would appriciate if you could word your answers a bit simpler i'm not that much of an expierenced programmer, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大多数具有公共属性(即读+写)的标准类型应该是可编辑的。
如果
Vector2
相当简单,并且您只是希望它在PropertyGrid
中展开,那么:如果
Vector2
是您自己的代码,那么您可以还可以装饰Vector2
本身,并将应用于所有属性:对于控件外部的类型,还有一个技巧可以做到这一点;在应用程序启动时,运行:
Most standard types with a public property (that is read+write) should be editable.
If
Vector2
is fairly simple, and you just want it to expand in thePropertyGrid
, then:If
Vector2
is your own code, then you can also decorateVector2
itself and it will apply to all properties:There is also a trick to do this for types outside of your control; at app startup, run:
您需要为
Vector2
类创建自定义 UI 类型编辑器。这是一个教程。
You need to create a custom UI Type Editor for your
Vector2
class.Here's a tutorial.
我在代码中没有看到任何解释这一点的内容。您不需要执行任何特殊操作即可使
int
或string
属性在 propertygrid 中可编辑,如以下最小示例所示:我可以使用上述代码编辑所有三个属性。您如何确定“无法改变价值观”?你到底看到了什么?
I don't see anything in your code which explains this. You do not need to do anything special to make
int
orstring
properties editable in a propertygrid, as demonstrated by this minimal example:I can edit all three properties with the above code. How did you determine that you "cannot change values"? What are you seeing exactly?
我修好了,我有 this.KeyPreview = true;在我的 Form1 构造函数中,通过删除它我可以解决问题。
感谢您的帮助!
I fixed it, i had this.KeyPreview = true; in my Form1 constructor, by removing that i could fix the issue.
Thanks for all your help!