Search code examples
.netpropertygridcollectioneditor

How do you make a CollectionEditor modeless?


Using a PropertyGrid, how can I make the collection editor that opens when I click a list parameter become modeless instead of modal?


Solution

  • Create a new type of CollectionEditor:

    public class SmartCollectionEditor : CollectionEditor
    {
    

    Override the 'EditValue' form to open the collection form modelessly instead of modal, and remove the accept and cancel buttons because changes will take effect immediately.

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
            CollectionForm collectionForm = base.CreateCollectionForm();
        collectionForm.EditValue = value;
        (collectionForm.AcceptButton as Button).Visible = false;
        (collectionForm.CancelButton as Button).Visible = false;
        collectionForm.Show();
        return value;
    }
    

    Mark the types that you want to use this with an attribute that will make the PropertyGrid open it.

    TypeDescriptor.AddAttributes(typeof(List<MyType>), new EditorAttribute(typeof(SmartCollectionEditor), typeof(UITypeEditor)));