简体   繁体   中英

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 */ );
    }
}

您可以创建对话框的属性以获取/设置List <>数据。

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM