windows 以编程方式隐藏 PropertyGrid 中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2319914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically hiding properties in a PropertyGrid
提问by tgandrews
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.
我希望根据用户在 drop 中所做的选择来隐藏/显示属性。我正在很好地处理事件,但实际上无法使正确的属性消失。我在网上找到的解决方案主要来自 2005 年,由于我对它的经验很少,我认为如果隐藏属性如此困难,我一定做错了什么。
So far I have tried accessing the property once the event is handled but the ReadOnly and IsBrowsable properties are read only.
到目前为止,我已经尝试在处理事件后访问该属性,但 ReadOnly 和 IsBrowsable 属性是只读的。
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.
propertygrid 有一个属性 BrowsableAttributes,它接受一个 Attribute 列表,但只能消极地工作,不能做 or - only and。提供一个属性集合;类别 - 'test' 和 isbrowsable - true;返回匹配两者的那些,并且由于我无法提供多个 AttributeCollections 我无法使搜索足够具体以隐藏必要的,同时让其他人可见。
I have been banging my head against a wall for the past couple of hours thinking there must be an easier way.
在过去的几个小时里,我一直在用头撞墙,认为一定有更简单的方法。
回答by t0mm13b
Have you tried applying this attribute to a property:
您是否尝试将此属性应用于属性:
[Browsable(false)] public object SomeProperty{ }
In that way SomeProperty
will not appear on the propertygrid.
这样SomeProperty
就不会出现在propertygrid上。
回答by Paul Sainsbury
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.
要在这里做你想做的事情,你需要在你的类上实现 ICustomTypeDescriptor,这是 PropertyGrid 将用来查询你的类/对象以找出要在 PropertyGrid 中显示的项目,如何编辑它们,什么他们应该拥有的类别/描述等。
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.
它可能会变得非常混乱,但似乎在http://www.codeproject.com/KB/grid/PropertyGridDynamicProp.aspx上的 Code Project 上有一组预先编写好的帮助程序类。
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.
如果它不能解决您的问题,请告诉我,我会更深入地挖掘。