开发者

Winforms PropertyGrid - properties not editable

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 s开发者_高级运维creenpos { 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.


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


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

Here's a tutorial.


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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜