Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

.net - Programmatically Hide Field in PropertyGrid

Using

<System.ComponentModel.TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter))> _

on the declaration of a class (which is a property of another class) that consists of a number properties.

I load an instance of this class with simply ...

PropertyGrid1.SelectedObject = oColumn

Obviously I don't want to manually build the propertygrid in code, I know how to do that.

But here's the problem. Depending on the value of a property, certain other properties should not be visible, as though I'd used the

<System.ComponentModel.Browsable(False)> _

attribute on the property declaration.

Is there anyway to do this programmatically, without having to handle all the building of the property grid manually>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...