Live Chat Icon For mobile
Live Chat Icon

How can I let the user edit my Multi-Dimensional collection?

Platform: WinForms| Category: Type Editors

This is usually a problem if you have a custom collection type MyCollection which itself can take items of type MyCollection.

The problem is because a single instance of a UITypeEditor is used by the framework irrespective of how many types and instances it serves to edit. Which means you cannot start editing your MyCollection from within a MyCollection editor, since the editor is already open.

To work around this problem, you can provide a custom editor as follows:


public class CustomCollectionEditor : CollectionEditor
{
	// The base class has its own version of this property
	// cached CollectionForm
	private CollectionForm collectionForm;

	public CustomCollectionEditor(Type type)
		: base(type)
	{
	}

	public override object EditValue(
		ITypeDescriptorContext context, IServiceProvider provider, 
		object value) 
	{
		if(this.collectionForm != null && this.collectionForm.Visible)
		{
			// If the CollectionForm is already visible, then create a new instance
			// of the editor and delegate this call to it.
			BarItemsCollectionEditor editor = new BarItemsCollectionEditor(this.CollectionType);
			return editor.EditValue(context, provider, value);
		}
		else return base.EditValue(context, provider, value);
	}

	protected override CollectionForm CreateCollectionForm()
	{
		// Cache the CollectionForm being used.
		this.collectionForm = base.CreateCollectionForm();
		return this.collectionForm;
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.