Windows Develop Bookmark and Share   
 index > Windows Forms Designer > ExpandableObjectConverter - given context, obtain PropertyGrid.SelectedObject ?
 

ExpandableObjectConverter - given context, obtain PropertyGrid.SelectedObject ?

I have a subclass of ExpandableObjectConverter that overrides the GetStandardValues method. From the context object passed into GetStandardValues, can I get back to the PropertyGrid's SelectedObject property ?

The context object is an ITypeDescriptorContext - specifically a PropertyGridInternal.PropertyDescriptorGridEntry. That is, the "normal" property grid context object. I'm editing a sub-sub property, via the expandable object converter. In the QuickWatch window, I can see the object I want to reach via either of these expressions:

((System.Windows.Forms.PropertyGridInternal.GridEntry)(context)).OwnerGrid.SelectedObject
((System.Windows.Forms.PropertyGridInternal.GridEntry)(context)).ParentGridEntry.ParentGridEntry.Instance

but if I use those in my code I get the compile error 'System.Windows.Forms.PropertyGridInternal.GridEntry' is inaccessible due to its protection level. The help file also says don't use the PropertyGridInternal namespace, as it's just for internal use.

So, how do I obtain the PropertyGrid's SelectedObject property within GetStandardValues ? One possible hack to get at the selected object is using reflection to obtain the private OwnerGrid property:


Object selectedObject;
PropertyInfo piOwnerGrid = context.GetType().GetProperty("OwnerGrid");
Object grid = piOwnerGrid.GetValue(context, null);
if ((grid != null) && (grid is System.Windows.Forms.PropertyGrid))
selectedObject = ((PropertyGrid)grid).SelectedObject;
else
selectedObject = null;


but I'm sure I shouldn't be doing this. Is there a better way ?

Thanks,
Andy Mackie.
[2nd post - no response in Windows Forms General]

Andrew Mackie  Monday, September 25, 2006 9:23 AM
I'm sure you've thought of this, but I'd like to hear your reasoning. Can you use the Instance property of the TypeDescriptorContext to get the SelectionService for the container? I haven't tried this but I would imagine it would work.
Quirk  Tuesday, September 26, 2006 4:25 PM

The context.Container property is null.

The context.Instance returns the object that is being edited (the sub-sub-property), but this doesn't have a SelectionService property (it's just a "normal object").

This isn't at design time. It's a Windows Form app that is using a PropertyGrid to edit a "normal object", by setting the SelectedObject property of the property grid. The property that is expanded two-levels has a TypeConverter that is a sub-class of ExpandableObjectConverter.

Where would I expect to find SelectionService in this case ?

Andrew Mackie  Wednesday, September 27, 2006 4:25 PM
Ah I didn't realize this was for runtime, you're right then. Can you describe what it is you're trying to do?  That is to say, in a bigger picture...I realize you want to get the SelectedObject property but can't.
Quirk  Wednesday, September 27, 2006 5:52 PM

sorry for the delay ...

It's a data-transformation application, where the user defines various data sources\destinations (e-mail, file, mq etc.) and transformations.

The user selects a visual object in a diagram. This sets the selected object property of the grid. One of the properties of the object is expandable a couple of levels, and has a GetStandardValues. The content of the standard-values list can change, depending on certain other values from the "main object" (i.e. the grid's selected object), hence the need to reach back to the selected object, to know what is allowed in the list.

The reflection technique was the only wayI could get it working.

My main concern at present is "does this really work". Iknow it's a bit of a hack, but is this going to blow up in my face, or is this property "safe" to access within GetStandardValues ? (i.e. it will always refer to the selected object of the property grid at that particular time)

Thanks,
Andy.

Andrew Mackie  Friday, October 13, 2006 3:35 PM

You can use google to search for other answers

Custom Search

More Threads

• CodeDomSerializer and resx
• listbox ownerdraw doesn't redraw when multiselect is on
• Hosting the designer
• Problem in DoDragDrop in Custom form Designer on DotNet 2.0.
• cannot access a disposed object Object name: 'Microsoft.VisualStudio.Design.VSDedignSurface'
• Textbox border colour
• Code Editor Control
• Simple One: Pass Information From Custom Control Back Up Chain
• Custom Collection Editor + Drop Down
• Problem after hiding an owned form