[英]C# - DataSource of comboBox1 depended on comboBox2 value
我按照下面創建了兩個類:
class City
{
public string CityName { get; set; }
public string Region { get; set; }
public City(string sCountryName, string sLanguage)
{
this.CityName = sCountryName;
this.Region = sLanguage;
}
public override string ToString()
{
return CityName;
}
class Country
{
public string CountryName { get; set; }
public City[] cities;
public Country(string sCountryName, City[] sCities)
{
this.CountryName = sCountryName;
this.cities = sCities;
}
之后,我按如下所示創建了usa_cities和german_cities的對象:
City[] usa_cities = new City[] {new City("Washington", "English"),
new City("New York", "English"),
new City("San Francisco", "English") };
City[] german_cities = new City[] {new City("Berlin", "German"),
new City("Hamburg", "German"),
new City("Frankfurt", "German") };
然后按如下方式創建對象“國家”:
Country[] countries = new Country[] {new Country("USA", usa_cities),
new Country("Germany", german_cities) };
在我的應用程序中,我有兩個組合框。 第一個組合框允許用戶按照以下方式選擇國家/地區:
comboBox1.DataSource = countries;
comboBox1.DisplayMember = "CountryName";
comboBox1.ValueMember = "CountryName";
comboBox1.SelectedItem = -1;
我想根據comboBox1中選擇的值來設置comboBox2.DataSource。 例如,用戶在第一個組合框中選擇“美國”,第二個組合框應填寫usa_cities。 如果用戶選擇“德國”,則第二個組合框應填充“ german_cities”。
您能告訴我該怎么做嗎?
謝謝,
您應該添加的事件處理程序ComboBox1.SelectedIndexChanged
事件,你再使用更改ItemsSource
根據的情況下ComboBox2的ComboBox1.SelectedIndex
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.