Winforms PropertyGrid - 属性不可编辑

发布于 2024-09-30 10:27:38 字数 905 浏览 8 评论 0原文

您好,这是我关于堆栈溢出的第一个问题,所以如果我做了任何愚蠢的事情,请原谅我。 好吧,我的问题是我正在使用关卡编辑器,我想使用 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 技术交流群。

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

发布评论

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

评论(4

可爱咩 2024-10-07 10:27:38

大多数具有公共属性(即读+写)的标准类型应该是可编辑的。

如果 Vector2 相当简单,并且您只是希望它在 PropertyGrid 中展开,那么:

[Description("Defines the Position on the screen")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public Vector2 screenpos { get;  set; }

如果 Vector2 是您自己的代码,那么您可以还可以装饰 Vector2 本身,并将应用于所有属性:

[TypeConverter(typeof(ExpandableObjectConverter))]
public {class|struct} Vector2 {...}

对于控件外部的类型,还有一个技巧可以做到这一点;在应用程序启动时,运行:

TypeDescriptor.AddAttributes(typeof(Vector2),
    new TypeConverterAttribute(typeof(ExpandableObjectConverter)));

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 the PropertyGrid, then:

[Description("Defines the Position on the screen")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public Vector2 screenpos { get;  set; }

If Vector2 is your own code, then you can also decorate Vector2 itself and it will apply to all properties:

[TypeConverter(typeof(ExpandableObjectConverter))]
public {class|struct} Vector2 {...}

There is also a trick to do this for types outside of your control; at app startup, run:

TypeDescriptor.AddAttributes(typeof(Vector2),
    new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
初见 2024-10-07 10:27:38

您需要为 Vector2 类创建自定义 UI 类型编辑器。

这是一个教程

You need to create a custom UI Type Editor for your Vector2 class.

Here's a tutorial.

独﹏钓一江月 2024-10-07 10:27:38

我遇到的问题是我无法更改值,除非它是布尔值

我在代码中没有看到任何解释这一点的内容。您不需要执行任何特殊操作即可使 intstring 属性在 propertygrid 中可编辑,如以下最小示例所示:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
   static class Program
   {
      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new MainForm());
      }
   }

   public class Test
   {
      public string StringProperty { get; set; }
      public int IntProperty { get; set; }
      public bool BoolProperty { get; set; }
   }

   class MainForm : Form
   {
      public MainForm()
      {
         var propertyGrid = new PropertyGrid() { Dock = DockStyle.Fill };
         this.Controls.Add(propertyGrid);
         propertyGrid.SelectedObject = new Test();
      }
   }
}

我可以使用上述代码编辑所有三个属性。您如何确定“无法改变价值观”?你到底看到了什么?

the problem i am expierencing is that I cannot change values unless it is a boolean

I don't see anything in your code which explains this. You do not need to do anything special to make int or string properties editable in a propertygrid, as demonstrated by this minimal example:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
   static class Program
   {
      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new MainForm());
      }
   }

   public class Test
   {
      public string StringProperty { get; set; }
      public int IntProperty { get; set; }
      public bool BoolProperty { get; set; }
   }

   class MainForm : Form
   {
      public MainForm()
      {
         var propertyGrid = new PropertyGrid() { Dock = DockStyle.Fill };
         this.Controls.Add(propertyGrid);
         propertyGrid.SelectedObject = new Test();
      }
   }
}

I can edit all three properties with the above code. How did you determine that you "cannot change values"? What are you seeing exactly?

遥远的绿洲 2024-10-07 10:27:38

我修好了,我有 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!

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