使用 PropertyGrid 的 TypeDescriptor 动态添加属性级属性
我想在网格中显示时动态添加属性(如 Browsable(false))。场景是我有一个带有一些公共属性的自定义类。但是,我不希望所有公共属性都显示在 UI 中。有出路吗?
我尝试使用 TypeDescriptor。但是,它仅适用于类型级别,而不适用于属性级别。我找不到有关如何在属性级别使用它的任何帮助。
任何指示都将受到高度赞赏。
I want to add attributes (like Browsable(false)) dynamically while displaying in the grid. Scenario is that I have a custom class with some public properties. However, I dont want all of the public properties to be displayed in the UI. Is there a way out?
I tried using TypeDescriptor. However, it works only at the type level and not at the property level. I couldn't find any help on how to use it at property-level.
Any pointers will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
过去几天我一直在同一地区工作。长话短说,我得出的结论是这是不可能的。
您尝试在运行时应用属性的原因是因为您不想将“UI”代码与自定义类混合吗?
如果是这种情况,则
BrowsableAttribute
是 ComponentModel 的成员 (http://msdn.microsoft.com/en-us/library/system.componentmodel.aspx),并且与 WinForms 的 UI 相关性不同。卡尔
I have been working in this same area for the past few days. Long story short, I came to the conclusion this is not possible.
Is the reason you are attempting to apply attributes at runtime because you dont want to mix "UI" code with your custom classes?
If that is the case, the
BrowsableAttribute
is a member of the ComponentModel (http://msdn.microsoft.com/en-us/library/system.componentmodel.aspx) and is not UI related in the same sense as WinForms.Karl
实际上这是可能的,但我不得不承认它相当晦涩且缺乏记录。关键是从 System.ComponentModel.TypeDescriptionProvider 派生您自己的自定义 TypeDescriptorProvider。然后你可以返回你自己的 TypeDescriptor 后代。
我这样做是为了修复 ASP.NET MVC 中关于 ViewModel 和元数据的一个相当令人讨厌的限制,但您也可以使用它来插入您自己的额外元数据。
Actually It is possible, but I have to admit it is rather obscure and poorly documented. The key is to derive your own custom TypeDescriptorProvider from System.ComponentModel.TypeDescriptionProvider. Then you can return your own TypeDescriptor descendant.
I did this to fix a rather nasty limitation regarding ViewModels and metadata in ASP.NET MVC, but you can just as well use it to insert your own extra metadata.