Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 384
Alexander Karsten Avatar asked Nov 03 '10 13:11

Alexander Karsten


1 Answers

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)));
like image 189
Marc Gravell Avatar answered Oct 17 '22 22:10

Marc Gravell