Pass data from parent to dialog box
I have a combobox in a dialog box form. I need to fill this combo with 开发者_如何学编程the List<> from parent form. How to do that as I cannot pass the List<> via dialog box constructor.
frmChild frm = new frmChild();
frm.ShowDialog();
You can add a property or method on your form, that takes the List<items>
and populates the ComboBox.
For example:
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 */ );
}
}
You may make a property of your dialog to get/set List<> data.
You can add a property or method on your form, that takes the List and populates the ComboBox
Then overload the constructor.
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.
}
}
Both ways are okay.
{enjoy}
精彩评论