2009-03-01 46 views
85

我想連接綁定源類對象的列表,然後對象的值組合框任何人都可以建議如何做到這一點如何將列表綁定到組合框? (的WinForms)

public class Country 
    { 
     public string Name { get; set; } 
     public IList<City> Cities { get; set; } 

     public Country() 
     { 
      Cities = new List<City>(); 
     } 
    } 

是我的課,我想結合它的名字場綁定源它是用組合框,然後進行相關的

+0

的WinForms我想是幫我的數據值連接在國家物體靜止名現場我會弄清楚 – Mobin 2009-03-02 00:14:58

回答

129

當你指的是一個組合框,我假設你不希望使用2路數據綁定(如果是這樣,看看使用BindingList

public class Country 
{ 
    public string Name { get; set; } 
    public IList<City> Cities { get; set; } 
    public Country(string _name) 
    { 
     Cities = new List<City>(); 
     Name = _name; 
    } 
} 



List<Country> countries = new List<Country> { new Country("UK"), 
            new Country("Australia"), 
            new Country("France") }; 

bindingSource1.DataSource = countries; 

comboBox1.DataSource = bindingSource1.DataSource; 

comboBox1.DisplayMember = "Name"; 
comboBox1.ValueMember = "Name"; 
+3

謝謝,但有點問題,在運行應用程序 – Mobin 2009-03-02 00:30:51

+1

我沒有在組合框中看到名稱我剛剛創建了一個WinForms應用程序與該代碼,它工作正常。 – 2009-03-02 00:32:39

+21

@Mobin如果名稱不可見,請確保您將DisplayMember綁定到類的屬性上,而不是公共字段。如果你的類使用`public string Name {get;組; }`它會工作,但如果它使用`public string Name';`它將無法訪問該值,而是顯示組合框中每行的對象類型。 – 2010-06-30 21:13:23

0

嘗試這樣:

yourControl.DataSource = countryInstance.Cities; 

如果您使用的WebForms,您將需要添加此行:

yourControl.DataBind(); 
+1

,不使用bindingsource ... – 2009-03-02 00:04:41

+0

這就是我要求的? – Mobin 2009-03-02 00:08:07

+1

以及comboBox1.DataBind();函數我沒有看到它在解決方案 我使用winforms – Mobin 2009-03-02 00:10:05

20

對於一個背景介紹,有2種方式使用ComboBox/ListBox中

1)增加國家的OBJ影響項目屬性並將國家檢索爲Selecteditem。要使用這個,你應該重寫國家的ToString。

2)使用數據綁定,設置數據源的IList的(名單<>),並使用DisplayMember,ValueMember和的SelectedValue

對於2),就需要先

// not tested, schematic: 
List<Country> countries = ...; 
...; // fill 

comboBox1.DataSource = countries; 
comboBox1.DisplayMember="Name"; 
comboBox1.ValueMember="Cities"; 

國家的列表,然後在的SelectionChanged,

if (comboBox1.Selecteditem != null) 
{ 
    comboBox2.DataSource=comboBox1.SelectedValue; 

} 
0
public class Country 
    { 
     public string Name { get; set; } 
     public IList<City> Cities { get; set; } 

     public Country() 
     { 
      Cities = new List<City>(); 
     } 
    } 

    public class City { public string Name { get; set; } } 

List<Country> Countries = new List<Country> 
     { 
      new Country 
      { 
       Name = "Germany", 
       Cities = 
       { 
        new City {Name = "Berlin"}, 
        new City {Name = "Hamburg"} 
       } 
      }, 
      new Country 
      { 
       Name = "England", 
       Cities = 
       { 
        new City {Name = "London"}, 
        new City {Name = "Birmingham"} 
       } 
      } 
     }; 

     bindingSource1.DataSource = Countries; 
     member_CountryComboBox.DataSource = bindingSource1.DataSource; 
     member_CountryComboBox.DisplayMember = "Name"; 
     member_CountryComboBox.ValueMember = "Name"; 

這是代碼我現在使用

16
public MainWindow(){ 
    List<person> personList = new List<person>(); 

    personList.Add(new person { name = "rob", age = 32 }); 
    personList.Add(new person { name = "annie", age = 24 }); 
    personList.Add(new person { name = "paul", age = 19 }); 

    comboBox1.DataSource = personList; 
    comboBox1.DisplayMember = "name"; 

    comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged); 
} 


void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    person selectedPerson = comboBox1.SelectedItem as person; 
    messageBox.Show(selectedPerson.name, "caption goes here"); 
} 

繁榮。

-2

如果您使用的是ToolStripComboBox控件沒有暴露的數據源(.NET 4.0):

List<string> someList = new List<string>(); 
someList.Add("value"); 
someList.Add("value"); 
someList.Add("value"); 

toolStripComboBox1.Items.AddRange(someList.ToArray());