2010-08-13 101 views
3

我在對話框窗體中有一個組合框。我需要填寫這個組合,其中包含來自父表單的列表<>。如何做到這一點,因爲我不能通過對話框構造函數通過列表<>。將父數據傳遞給對話框

frmChild frm = new frmChild(); 
frm.ShowDialog(); 

回答

7

您可以在窗體上添加屬性或方法,該窗體需要List<items>並填充組合框。

例如:

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

你可以讓你的對話獲得/設置列表<>數據的屬性。

0

您可以 添加屬性或方法的形式,即採取列表 並填充組合框

然後重載構造函數。

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

這兩種方法都還好。

{享受}

+0

第一種選擇是不是世界上最好的主意,有一個子窗口「神交」回到其父只是自尋煩惱,如果你到別的地方重新使用的窗口,第二個選項,或者一個屬性/方法是更好的選擇 – Rob 2010-08-13 13:43:05

+0

你是對的,我的意思是你使用第一種方法獲得額外的開銷。不過,我使用第一種方法一次使用自定義窗口在窗口打開時爲調用者​​設置模糊效果。它工作得很好。 – 2010-08-13 14:23:43

相關問題