以编程方式隐藏 PropertyGrid 中的属性
我希望根据用户在下拉菜单中所做的选择来隐藏/显示属性。我很好地处理了该事件,但实际上无法使正确的属性消失。我在网上找到的解决方案主要来自 2005 年,由于我对此的经验很少,我想如果隐藏属性这么难,我一定做错了什么。
到目前为止,我已尝试在处理事件后访问该属性,但 ReadOnly 和 IsBrowsable 属性是只读的。
propertygrid 有一个属性 BrowsableAttributes,它接受一个属性列表,但只能负作用,不能做或 - 只能做和。提供属性集合;类别 - '测试' 和 isbrowsable - true;返回那些与两者匹配的属性,并且由于我无法提供多个 AttributeCollections,因此我无法使搜索足够具体以隐藏必要的属性,同时使其他属性可见。
在过去的几个小时里,我一直在用头撞墙,认为一定有一种更简单的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将此属性应用于属性:
这样
SomeProperty
将不会出现在属性网格上。Have you tried applying this attribute to a property:
In that way
SomeProperty
will not appear on the propertygrid.要在这里执行您想要执行的操作,您需要在您的类上实现 ICustomTypeDescriptor,PropertyGrid 将使用它来查询您的类/对象,以找出要在 PropertyGrid 中显示的项目、如何编辑它们、什么他们应该有的类别/描述等。
它可能会变得非常混乱,但似乎在代码项目上有一组预先编写的帮助器类,网址为 http://www.codeproject.com/KB/grid/PropertyGridDynamicProp.aspx。
我开始编写自己版本的代码项目中给出的代码,在遇到障碍后,我进行了一些谷歌搜索并找到了代码项目链接。看起来他们做得比我好很多。我已经下载了代码,它似乎工作得很好。
如果它不能解决您的问题,请告诉我,我会更深入地研究。
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.