views:

439

answers:

1

I am using a PropertyGrid to show properties from my objects. However, I'm also allowing the user to create their own properties, and set values for these custom properties. Each object that can have these custom properties has a Dictionary collection, where the string is a unique key to identify the property, and Object is the value of a primitive type (string, bool, int etc..)

I've created a custom PropertyDescriptor with get and set methods that check the Dictionary for a matching key, or create/overwrite the value with a matching key respectively.

However, I also want to give the user the ability to clear the property, and thus remove its entry from the dictionary entirely. I'd put the code to to this in the ResetValue override method of my custom PropertyDescriptor, however I don't see any way through the PropertyGrid interface to envoke this? It doesn't seem to be a context menu option or something obvious like that.

So if I have a custom PropertyDescriptor with a custom ResetValue method, how do I actually evoke that method from a PropertyGrid?

+2  A: 

I think the easiest way to achieve this is to add a contextmenu to your property grid, with a menu item "Reset", and then handling its click event like this:

private void resetToolStripMenuItem_Click(object sender, EventArgs e)
{                        
    PropertyDescriptor pd = propGrid.SelectedGridItem.PropertyDescriptor;
    pd.ResetValue(propGrid.SelectedObject);
}

I think Visual Studio does something like this.

Daniel LeCheminant
Ahh yeah I just found this post on MSDN as well. Seems like that's the best option.http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9dd7b9bf-4134-4105-aba8-65002fed04f2
Eric Anastas
Yeah, the call to propGri.SelectedGridItem.Select() seems like it would clear up some update issues as well.
Daniel LeCheminant