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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
tgandrews
  • 12,349
  • 15
  • 43
  • 55
  • This is similar to this [question](http://stackoverflow.com/questions/626803/programatically-hide-field-in-propertygrid ), which some people may wish to cross reference, but this answer seems more complete. – Charley Rathkopf Jul 15 '10 at 21:33

2 Answers2

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.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • Yes that will work at first but I need to be able to disable/enable when the drop down value changes. – tgandrews Feb 23 '10 at 16:55
  • @Tom: Incidentally, did you know that you can apply a custom Description to the property ... '[Description("some description for SomeProperty")]'.... – t0mm13b Feb 23 '10 at 17:02
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.

Paul Sainsbury
  • 429
  • 1
  • 4
  • 8