Programmatically hiding properties in a PropertyGrid
Asked Answered
C

2

3

I'm looking to hide/show properties depending on what selection the user makes in a drop. I am handling the event fine, but cannot actually make the correct properties disappear. The solutions I have found on line are mainly from 2005 and as I've had very little experience with it I thought I must be doing something wrong if hiding properties is this hard.

So far I have tried accessing the property once the event is handled but the ReadOnly and IsBrowsable properties are read only.

The propertygrid has a property BrowsableAttributes that takes an Attribute list but only works negatively and cannot do or - only and. Providing an attribute collection of ; category - 'test' and isbrowsable - true; returns those that match both and as I can't provide multiple AttributeCollections I cannot make the search specific enough to hide the necessary ones whilst leaving others visible.

I have been banging my head against a wall for the past couple of hours thinking there must be an easier way.

Coates answered 23/2, 2010 at 16:38 Comment(1)
This is similar to this [question](#627303 ), which some people may wish to cross reference, but this answer seems more complete.Gorlin
D
14

Have you tried applying this attribute to a property:

[Browsable(false)]
public object SomeProperty{
}

In that way SomeProperty will not appear on the propertygrid.

Deidredeific answered 23/2, 2010 at 16:50 Comment(2)
Yes that will work at first but I need to be able to disable/enable when the drop down value changes.Coates
@Tom: Incidentally, did you know that you can apply a custom Description to the property ... '[Description("some description for SomeProperty")]'....Deidredeific
L
4

To do what you want to do here, you'd need to implement ICustomTypeDescriptor on your class, which is what the PropertyGrid will use to query your class/object to find out what items to display in the PropertyGrid, how to edit them, what category/description they should have, etc.

It can get quite messy, but it seems that there's a pre-written set of helper classes for you on Code Project at http://www.codeproject.com/KB/grid/PropertyGridDynamicProp.aspx.

I'd started writing my own version of the code given at Code Project and after hitting a snag I did some googling and came up with the Code Project link. It looks like they've done a lot better than I was. I've downloaded the code and it seems to work quite well.

If it doesn't solve your problem, please let me know and I'll dig a bit deeper.

Leesaleese answered 23/2, 2010 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.