How do I add a service to a property grid type descriptor context in .Net?
I have an application that allows the user to select an image at design time, either as a direct image or from a list of images.
So far everything's cool except it doesn't come from the visual studio property browser, it comes from the property grid which is part of the type editor.
My problem is that both the image collector (actually the resource collector) and the imagelist type converter rely on some design-time services to get the job done. In the case of imagelist, its IReferenceService and in case of resource selection, its service is called _DTE.
In the first instance of editing from the visual studio property browser, I could get a reference to these services, but (1) how can I add them to the type descriptor context of my property grid?
For future verification, it would be better if I could just copy the reference to all services in the context of the type descriptor. (2) Where does the property browser get these services in the first place?
ETA: I still don't know how to do it, but now I know that it is possible.
(1) Subclass management and add a property whose type is an array of buttons.
(2) Add it to the form.
(3) Select a new control in the design service and edit the new property in the property browser.
(4) The Collection Editor dialog box
appears
(5) Add Button
(6) Edit Image and Image List - the type editor and type converter, respectively, behave as they should.
ETA2: Okay, I'm getting warm. It looks like you are doing this through the Site property of the property grid. I can create a new site and pass it the context of the type descriptor to which I have a link, and then it should have the whole service. I'll give it ...
ETA3: Yes it is. I'll add an answer.
a source to share
Thanks to the reflector and structure of the CollectionEditor, I found the answer via the Site PropertyGrid. This is what you are doing:
(1) Save the reference to the ITypeDescriptor context. You can do this with the first edit from the Property Browser or Design Surface.
(2) After creating your own property grid, set the Site property to a new site containing all the services from the original context.
Instead of reinventing the wheel, use a reflector and go to System.ComponentModel.Design.CollectionEditor.PropertyGridSite and enter the code.
a source to share