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). Howeve开发者_如何学编程r, 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
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.
精彩评论