【问题标题】:Winforms PropertyGrid - properties not editableWinforms PropertyGrid - 属性不可编辑
【发布时间】:2010-11-03 13:18:38
【问题描述】:

您好,这是我的第一个堆栈溢出问题,如果我做了任何愚蠢的事情,请原谅我。 好吧,我的问题是我正在使用关卡编辑器,我想使用 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 的集合中删除私有,我也可以编辑它,但我不能编辑其他任何东西,哦,如果你能把你的答案说得简单一点,我会很高兴我没那么多一位经验丰富的程序员,在此先感谢。

【问题讨论】:

    标签: c# winforms properties propertygrid


    【解决方案1】:

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

    如果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)));
    

    【讨论】:

      【解决方案2】:

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

      Here's a tutorial.

      【讨论】:

        【解决方案3】:

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

        我在您的代码中没有看到任何解释这一点的内容。您无需执行任何特殊操作即可使 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();
              }
           }
        }
        

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

        【讨论】:

        • 我看到它们就像一个普通的属性网格,没有变灰任何我可以移动文本光标和东西的东西,但是如果我输入(字母或数字)没有任何反应。
        【解决方案4】:

        我修好了,我有 this.KeyPreview = true;在我的 Form1 构造函数中,通过删除我可以解决问题。 感谢您的所有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-10
          • 2023-04-10
          • 2011-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-03
          相关资源
          最近更新 更多