Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Add property-level attributes dynamically using TypeDescriptor for PropertyGrid
 

Add property-level attributes dynamically using TypeDescriptor for PropertyGrid

Hi All,

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. I also can't decorate the custom class properties with attributes as they are beyond my control. 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.

Thanks in Advance
Pavan

PavanKK  Thursday, August 20, 2009 4:45 AM

Hi PavanKK,

As far as I know, we cannot add an attribute to a property of a component, it is because the type of the component is certain during runtime. We can use CodeDom to dynamically generate source code to create a dynamic type, but we still cannot modify the type of some existent instances during runtime.

From your descript, what you want is to filter some properties when the component is edited in PropertyGrid. We can create our own PropertyGrid to do that. We can override the SelectedObject property and bind a wrapper class instead of the component. In the wrapper class, we would filter the properties. This is a sample shows a custom PropertyGrid which supports properties filtering:
http://www.codeproject.com/KB/cs/FilteredPropertyGrid.aspx.

Best regards,

Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 21, 2009 7:24 AM
Hi PavanKK,

Could you please let me know if my reply helps?

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 24, 2009 4:12 AM
Hi Aland,

Apologies for a delayed response. I was on a short vacation. I will check this out and let you know.

Meanwhile, I was trying to explore the option of implementing ICustomTypeDescriptor and TypeDescriptionProvider. Though I'm not very clear at this point if it really is going to help me or not, I'm going to give it a try too. Pls let me know what you think of this.

The scenario that I have is: I have some entities which are being bound to a property grid. However, I can't use property grid specific attributes like browsable,categoryetc on them as I would reuse them and don't want to mix UI related stuff with my entities. However, while rendering on the property grid I would want these attributes to be applied (or rather I would say, I want some public properties from my entities browsable and some don't). Is this scenario achievable by overriding SelectedObject in the propertygrid?

Appreciate your help.

Regards,
Pavan
PavanKK  Tuesday, August 25, 2009 10:37 AM

Hi PavanKK,

You said: I was trying to explore the option of implementing ICustomTypeDescriptor and TypeDescriptionProvider. Though I'm not very clear at this point if it really is going to help me or not, I'm going to give it a try too. Pls let me know what you think of this.

Based on my understanding, it would help, you can try. In fact, the custom PropertyGrid at the link I provided binds an object which implements ICustomDescriptor to filter the properties of the selected object.

You also said: Is this scenario achievable by overriding SelectedObject in the propertygrid?

Yes, the custom PropertyGrid use this method to hide some public properties. You can read the code of it to get the details.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, August 25, 2009 10:55 AM
Hi PavanKK,

This thread shows some ideas : http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/1a155f2e-c6af-47ed-bc03-ea751c937045.

Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, August 25, 2009 12:09 PM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 27, 2009 8:44 AM

You can use google to search for other answers

Custom Search

More Threads

• ContextMenuStrip behavior on a TreeView (repost)
• fake custom property - designer notification
• I need the CheckBox portion of the CheckBox control to be smaller, say 2/3 the size - How can I do that
• Mouse monitoring.(monitoring key Clicks)
• treeview
• Second screen
• How do I get the accesskey functionality to work in a Toolstrip container containing my custom component?
• Make sort kind of DataGridViewColumns when click on HeaderColumns
• Add a Images property to ListView
• .NET erasing my hand written code!?! Please help