Programmatically Hide Property in PropertyGrid
Asked Answered
I

4

3

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>

Isisiskenderun answered 9/3, 2009 at 15:50 Comment(0)
B
1

if you were hoping for a gridItem.Hide() then, the answer is no. The only way to achieve that in the MS PropertyGrid is to dynamically publish your properties through the GetProperties method of a TypeConverter or custom type descriptor (that implements ICustomTypeDescriptor). I would try first with the TypeConverter (especially if the property values you want to check are at the same level), there is less coding to do.

Byrn answered 10/3, 2009 at 14:22 Comment(0)
A
16

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.

Adulthood answered 31/7, 2009 at 1:40 Comment(2)
great hint with the BrowsableAttributes if you do not care for the sort order.Harvard
this is exactly the solution I was looking for. Thanks a ton bro. To hide appearance, you will need to do: [Browsable(false)] [Category("Design")] public new Appearance Appearance { get { return base.Appearance; } set { base.Appearance = value; } }Jillian
B
1

if you were hoping for a gridItem.Hide() then, the answer is no. The only way to achieve that in the MS PropertyGrid is to dynamically publish your properties through the GetProperties method of a TypeConverter or custom type descriptor (that implements ICustomTypeDescriptor). I would try first with the TypeConverter (especially if the property values you want to check are at the same level), there is less coding to do.

Byrn answered 10/3, 2009 at 14:22 Comment(0)
V
0

As for the C++, here is an easy solution to show selected category in a propertyGrid.

cli::array<Attribute^,1>^ attrs = {gcnew CategoryAttribute("Appearance")};
this->PropertyGrid->BrowsableAttributes = gcnew AttributeCollection(attrs);
this->PropertyGrid->SelectedObject = this->SelectedControl;

This will show only 'Appearance' category in the propertyGrid and hide all other categories. Assuming you can translate the code in C# yourself.

Vincenz answered 17/4, 2013 at 9:37 Comment(0)
A
-2

This Question is similar, but the answers are more complete. Some people may wish to cross reference.

Assort answered 15/7, 2010 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.