我在对话框表单中有一个下拉框,需要使用父表单的List<>填充该下拉框。由于无法通过对话框构造函数传递List<>,因此该如何实现呢?
frmChild frm = new frmChild();
frm.ShowDialog();
List<items>
并将其填充到 ComboBox 中。List<ItemType> items = GetItemsForFormsComboBox();
frmChild frm = new frmChild();
frm.SetComboItems(items);
frm.ShowDialog();
// in the form
public void SetComboItems(List<ItemType> items)
{
foreach(var item in items)
{
myCombo.Add( /* construct combo item and use item to populate it here */ );
}
}
你可以将对话框的属性设置成获取/设置List<> 数据。
public class ComboBoxWindow : Window
{
public ComboBoxWindow (Window origin)
{
// Now you can access your parent window's List<>.
}
// If necessary you can keep a reference to it.
private Window _origin;
}
OR
public class ComboBoxWindow : Window
{
// If necessary you can keep a reference to it.
private IList _items;
public ComboBoxWindow (IList _items)
{
// Now you can access your list directly.
}
}
两种方式都可以。
{享受}