Add property-level attributes dynamically using TypeDescriptor for PropertyGrid
Asked Answered
M

2

6

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.

Mcalpin answered 19/8, 2009 at 14:23 Comment(1)
It's possible, I've provided full implementation in my question #12144150Karajan
D
2

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.

Denny answered 11/4, 2011 at 19:28 Comment(0)
I
3

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

Invincible answered 19/9, 2009 at 2:0 Comment(0)
D
2

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.

Denny answered 11/4, 2011 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.