How to open custom editor for property in PropertyGrid from the outside?

Custom Editor for a Boolean Property in a PropertyGrid (C#)

  • I want to assign a custom editor to a boolean property in a PropertyGrid. I'm using the standard propertygrid (from namespace System.Windows.Forms). It is possible to assign custom editors to properties using the UITypeEditor class. However, as far as I can see, it is not possible to use it for a boolean property. I've tried to solve it by overriding the property grid so I can add items manually. I can add a string property that has a custom editor by the following piece of code: Properties.Item.Add("My Custom Editor", "", false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true); Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor(); So far so good, a custom editor appears (with a button in the grid). However, when I change the type to boolean by setting the default value on false (see below), the button to open the custom editor doesn't appear. Instead, a dropdown menu with true/false appears. Properties.Item.Add("My Custom Editor", false, false, "Properties with custom UITypeEditor", "The component accept custom UITypeEditor.", true); Properties.Item[Properties.Item.Count - 1].CustomEditor = new MyEditor(); Does anyone has a solution for this? Thanks in forward! Regards, Peter

  • Answer:

    The Microsoft PropertyGrid checks this flag to determine if it displays a dropdown arrow (flag == true) or a modal button (flag == false): bool flag = gridEntryFromRow.NeedsDropDownButton | gridEntryFromRow.Enumerable; The first part is true if the UITypeEditor style is DropDown and the second part is true if the attached TypeConverter's GetStandardValuesSupported returns true. You can check all this in PropertyGridView.SelectRow in Reflector. If you are able to attach a custom TypeConverter to your boolean (I would derive it from BooleanConverter) whose GetStandardValuesSupported method is overriden to return false, then you will get your modal button. Of course you loose the standard values (double clicking won't cycle the values for example), this is a tradeoff. I have identified this issue a long time ago and that's why in my own PropertyGrid I am not so strict and will enable a modal editor even if standard values are defined as long as I attach a ForceEditor attribute to the property.

Peter van Kekem at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.