PropertyGrid will use default editor for the properties which’s type is enum. This default editor does not allow multiple selections even the Enum has Flags attribute.
I have been prepared a new Editor for this kind of properties. You can download the source codes from
here …

Our editor’s code is as follows;
internal class FlagsEditor : UITypeEditor { FlagsEditorControl editor = null; public FlagsEditor() { } // our editor is a DropDown editor public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { // if value is not an enum than we can not edit it if (!(value is Enum)) throw new Exception("Value doesn't support"); // try to figure out that is this a Flags enum or not ? Type enumType = value.GetType(); object[] attributes = enumType.GetCustomAttributes(typeof(FlagsAttribute), true); if (attributes.Length == 0) throw new Exception("Editing enum hasn;t got Flags attribute"); // check the underlying type Type type = Enum.GetUnderlyingType(value.GetType()); if (type != typeof(byte) && type != typeof(sbyte) && type != typeof(short) && type != typeof(ushort) && type != typeof(int)&& type != typeof(uint)) return value; if (provider != null) { // use windows forms editor service to show drop down IWindowsFormsEditorService edSvc = provider.GetService( typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (edSvc == null) return value; if (editor == null) editor = new FlagsEditorControl(this); // prepare list editor.Begin(edSvc, value); // show drop down now edSvc.DropDownControl(editor); // now we take the result value = editor.Value; // reset editor editor.End(); } return Convert.ChangeType(value, type); } } |
In this code FlagsEditorControl is a UserControl which PropertyGrid hosts it in a drop down during the edit operation.
You can change your design by changing this Control.
To use this editor for a property, we have to write Editor Attribute to that property as the below;
// set editor of this property to our FlagsEditor [Editor(typeof(FlagsEditor), typeof(UITypeEditor))] public FileAttributes FlagsAttribute { get { return _FlagsAttribute; } set { _FlagsAttribute = value; } } |