Pass data from parent to dialog

I have a combobox in the form of a dialog box. I need to populate this combination with List <> from parent form. How to do this since I cannot pass the List <> dialog box constructor through.

frmChild frm = new frmChild();
frm.ShowDialog();
+3
source share
3 answers

You can add a property or method to your form, which accepts List<items>and populates a ComboBox.

For instance:

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

You can make a property of your dialog to get / set List <> data.

+1
source

, ComboBox

.

public class ComboBoxWindow : Window
{
    public ComboBoxWindow (Window origin)
    {
        // Now you can access your parent window List<>.
    }

    // If necessary you can keep a reference to it.
    private Window _origin;
}

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.
    }
}

.

{}

0

Source: https://habr.com/ru/post/1759515/


All Articles