What is the easiest way to edit and persist a collection like decimal[]
or List<string>
in the WinForms designer?
The first problem is that a parameterless constructor is needed. So I made a simple wrapper class:
(at some point this was like MyObject<T>
, but the WinForms designercode generator didn't know how to handle it)
[Serializable()]
public class MyObject
{
public MyObject() {}
public decimal Value {get; set;}
}
In the container class we define a property and add the CollectionEditor attribute to it:
public class MyContainer
{
private List<MyObject> _col = new List<MyObject>();
[Editor(typeof(CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public List<MyObject> Collection
{
get { return _col; }
set { _col = value; }
}
}
Now I tried all sorts of things based on answers here on stackoverflow and articless on codeproject.com:
- ArrayEditor with decimal[] field
- Custom TypeConverter for MyObject
- Custom Collection class for List
- Read/Write property on the container class
- Custom Editor with implementation for EditValue
- Implement IComponent interface for MyObject
- Tried adding DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
I did get it to work so that
- The collection is visible in DesignMode
- The collection is editable in DesignMode
- New items can be added to the collection in DesignMode
However, by saving, closing and re-opening the form the elements in the collection are never persisted.
Edit: Hans gave me some tips (his comments somehow went into the void). I followed his guidelines and updated the source above, which unfortunately still doesn't work...