Programmatically hiding properties in a PropertyGrid

ぃ、小莉子 提交于 2019-12-13 14:19:49

问题


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.


回答1:


Have you tried applying this attribute to a property:

[Browsable(false)]
public object SomeProperty{
}

In that way SomeProperty will not appear on the propertygrid.




回答2:


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.



来源:https://stackoverflow.com/questions/2319914/programmatically-hiding-properties-in-a-propertygrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!