I have developped a component with a propriety of type List<BaseItem> where
BaseItem is a generic class. Two classes inherit from BaseItem : Noeud and
Item. Class Noeud has also a propriety of type List<BaseItem> so that we can
build a tree of BaseItem and assign it to the component. I have a custom
collection editor to edit the component's propriety (I gave up my first idea
to implement my own CollectionForm!).
I have a problem : there is a strange behavior when I want to add some
BaseItems to the second level of the tree. Let's take an example : if I
create an original tree like this : Noeud1(Noeud2,Item1), close all the
editor forms, and then, try to add an item in the collection of Noeud2,
nothing happens. But if I add a second item in Noeud1 first, and then create
an item in Noeid2, it works !
Can someone help me, please ?
Here's my code :
#region Items
public class BaseItem
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public class Noeud : BaseItem
{
private List<BaseItem> _items;
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)]
public List<BaseItem> Items
{
get { return _items; }
set { _items = value; }
}
public Noeud()
{
_items = new List<BaseItem>();
}
}
public class Item : BaseItem
{
private string _misc;
public string Misc
{
get { return _misc; }
set { _misc = value; }
}
}
#endregion
#region Editor
public class MyEditor : CollectionEditor
{
public MyEditor(Type type)
: base(type)
{ }
protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(Noeud), typeof(Item) };
}
}
#endregion
#region Component
public partial class MyComponent : Component
{
private List<BaseItem> _items;
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)]
public List<BaseItem> Items
{
get { return _items; }
set { _items = value; }
}
public MyComponent()
{
InitializeComponent();
_items = new List<BaseItem>();
}
public MyComponent(IContainer container)
{
_items = new List<BaseItem>();
container.Add(this);
InitializeComponent();
}
}
#endregion