Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “将数据从父级传递到”对话框_C#_.net_Combobox_Dialog - Fatal编程技术网

C# “将数据从父级传递到”对话框

C# “将数据从父级传递到”对话框,c#,.net,combobox,dialog,C#,.net,Combobox,Dialog,我有一个对话框形式的组合框。我需要用来自父窗体的列表填充此组合。如何做到这一点,因为我无法通过对话框构造函数传递列表 frmChild frm = new frmChild(); frm.ShowDialog(); public class ComboBoxWindow : Window { public ComboBoxWindow (Window origin) { // Now you can access your parent window's Lis

我有一个对话框形式的组合框。我需要用来自父窗体的列表填充此组合。如何做到这一点,因为我无法通过对话框构造函数传递列表

frmChild frm = new frmChild();
frm.ShowDialog();
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;
}

您可以在表单上添加属性或方法,该属性或方法接受
列表
并填充组合框

例如:

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 items=getitemsformscombobox();
frmChild frm=新的frmChild();
frm.SetComboItems(项目);
frm.ShowDialog();
//在形式上
公共void SetComboItems(列表项)
{
foreach(项目中的var项目)
{
myCombo.Add(/*构造组合项并使用该项在此处填充*/);
}
}

您可以创建对话框的属性来获取/设置列表数据


可以在上添加属性或方法 你的表格,那就是清单 并填充组合框

然后重载构造函数

frmChild frm = new frmChild();
frm.ShowDialog();
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;
}
两种方式都可以


{previous}

第一个选项并不是最好的主意,让子窗口“grok”回到其父窗口只是自找麻烦,如果您试图在其他地方重复使用该窗口,那么第二个选项或属性/方法是更好的选项。您是对的,我的意思是,使用第一个方法会增加额外的开销。尽管如此,我还是在自定义窗口中使用了第一种方法,在打开窗口时对调用者设置模糊效果。结果很好。