我有自定义集合编辑器,我想以编程方式将项目添加到它的列表(集合)中,以便它们可以在列表框中可见。我怎么能这样做?我知道 CollectionEditor 的 AddItems 方法,但它将集合对象作为参数,但我无法找到获取 CollectionEditor 的内部列表对象的方法...:/
[更新] 呃.. 正确的方法名称是 'SetItems' [/update]
[更新 2] 我正在尝试做的源代码...
public class MyCollectionEditor : CollectionEditor
{
private Type m_itemType = null;
public MyCollectionEditor(Type type)
: base(type)
{
m_itemType = type;
}
protected override CollectionForm CreateCollectionForm()
{
Button buttonLoadItem = new Button();
buttonLoadItem.Text = "Load from DB";
buttonLoadItem.Click += new EventHandler(ButtonLoadItem_Click);
m_collectionForm = base.CreateCollectionForm();
TableLayoutPanel panel1 = m_collectionForm.Controls[0] as TableLayoutPanel;
TableLayoutPanel panel2 = panel1.Controls[1] as TableLayoutPanel;
panel2.Controls.Add(buttonLoadItem);
return m_collectionForm;
}
private void ButtonLoadItem_Click(object sender, EventArgs e)
{
if (m_itemType.Equals(typeof(MyCustomCollection)))
{
MyCustomItem item = ...load from DB...
//definition: SetItems(object editValue, object[] value);
SetItems( -> what goes here?! <- , new object[] { item });
}
}
}
[/更新 2]