Programmatically Hide Field in PropertyGrid

后端 未结 4 688
失恋的感觉
失恋的感觉 2020-12-10 20:21

Using

 _

on the declaration of a class

4条回答
  •  猫巷女王i
    2020-12-10 20:56

    Actually this is entirely possible. The first and easiest way is to set the grid's BrowsableAttributes property:

    propGraph.BrowsableAttributes = new AttributeCollection(
        new Attribute[] 
        { 
            new CategoryAttribute("Appearance")
        });
    

    This will filter out all properties that do NOT match the attribute-types you supply. Unfortunately this is a positive filter rather than a negative filter which makes it less useful IMHO.

    Second, and equally easy, you can create a simple wrapper around the object you want to display in the PropertyGrid and re-define whatever properties you want to hide/etc. as passthrough properties:

    public class MyDerivedControl : public TextBox
    {
        [Browsable(false)]
        [Category("MyCustomCategory")]
        public new bool Enabled
        {
             get { return base.Enabled }
             set { base.Enabled = value; }
        }
    }
    

    Pop that into a property grid and the Enabled property will be hidden.

    Third, you can customize the PropertyGrid itself and get into the world of type descriptors and so forth, but if all you want to do is hide a couple properties, this is overkill.

    Hope this helps.

提交回复
热议问题