[英]cascading combobox in a propertygrid c#4.0
我正在構建一系列winforms應用程序,並且我需要在選擇國家城市列表時應該填充一個帶有2個組合“國家和城市”的propertyGrid。
我一直在尋找一個例子但找不到任何例子。
任何地方的任何鏈接或codesnippet?
非常感謝
你需要TypeConverters,一個用於城市,一個用於國家:
public class CountryCity {
[TypeConverter(typeof(CountryConverter))]
public string Country { get; set; }
[TypeConverter(typeof(CityConverter))]
public string City { get; set; }
private static List<CountryCity> cityList = new List<CountryCity>();
static CountryCity() {
cityList.Add(new CountryCity() { Country = "Germany", City = "Berlin" });
cityList.Add(new CountryCity() { Country = "Germany", City = "Hamburg" });
cityList.Add(new CountryCity() { Country = "Germany", City = "Munich" });
cityList.Add(new CountryCity() { Country = "US", City = "Atlanta" });
cityList.Add(new CountryCity() { Country = "US", City = "Chicago" });
cityList.Add(new CountryCity() { Country = "US", City = "Los Angeles" });
cityList.Add(new CountryCity() { Country = "US", City = "New York" });
}
public class CityConverter : TypeConverter {
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
List<string> cities = new List<string>();
CountryCity cc = context.Instance as CountryCity;
if (cc != null) {
if (cc.Country == null) {
cities.AddRange(cityList.Select(x => x.City));
} else {
cities.AddRange(cityList.Where(x => x.Country == cc.Country)
.Select(y => y.City));
}
}
return new StandardValuesCollection(cities);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
foreach (CountryCity cc in cityList) {
if (cc.City == (string)value) {
return cc.City;
}
}
}
return base.ConvertFrom(context, culture, value);
}
}
public class CountryConverter : TypeConverter {
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
List<string> items = cityList.Select(x => x.Country).Distinct().ToList();
return new StandardValuesCollection(items);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
foreach (CountryCity cc in cityList) {
if (cc.Country == (string)value) {
return cc.Country;
}
}
}
return base.ConvertFrom(context, culture, value);
}
}
}
供測試用:
propertyGrid1.SelectedObject = new CountryCity();
使用1個靜態函數創建一個類國家/地區,以檢索所有已知國家
class Countries
{
public string Country{get;set;}
public int CountryId {get;set;}
public static List<Countries>MyCountries()
{
var c1 = new Countries{ Country = "Belgium", CountryId = 1};
var c2 = new Countries{ Country = "Netherlands", CountryId = 2};
var result= new List<Countries>();
result.Add(c1);
result.Add(c2);
return result;
}
}
使用靜態函數創建一個類城市來檢索所有城市,使用另一個靜態函數來搜索第一個函數。
class Cities
{
public string City{get;set;}
public int CityId{get;set;}
public int CountryId{get;set;}
public static List<Cities>MyCities()
{
var c1 = new Cities{ CityId = 1, City= "Antwerp", CountryId = 1};
var c2 = new Cities{ CityId = 2, City= "Den Haag", CountryId = 2};
var c3 = new Cities{ CityId = 3, City= "Brussels", CountryId = 1};
var c4 = new Cities{ CityId = 4, City= "Rotterdam", CountryId = 2};
var c5 = new Cities{ CityId = 5, City= "Amsterdam", CountryId = 2};
var c6 = new Cities{ CityId = 6, City= "Hasselt", CountryId = 1};
var result= new List<Cities>();
result.Add(c1);
result.Add(c2);
result.Add(c3);
result.Add(c4);
result.Add(c5);
result.Add(c6);
return result;
}
public static List<Cities>SelectedCities(int countryId)
{
//Linq Example
var l = MyCities();
var result = from m in l
where m.CountryId = countryId
select m;
return result;
//you also could use a foreach or For loop
//var l = MyCities();
//var result = new List<Cities>();
//foreach(var item in l)
//{
// if(item.CountryId == countryId)
// {
// result.Add(item);
// }
// return result;
}
}
}
在您的表單或usercontrol中添加以下代碼:
public Form1()
{
InitializeComponent(); //initialises the component
PopulateCountryBox(); //populate the combobox
combobox1.SelectedIndexChanged += combobox1_SelectedIndexChanged; //Create the event
}
在form_load或其他東西中調用下面的函數來加載CountryBox
private void PopulateCountryBox()
{
var countryList = new List<Countries>();
combobox1.DataSource = countryList;
combobox1.DisplayMember = "Country";
combobox1.ValueMember = "CountryId";
}
代碼下方將處理combobox1 selectedindexChanged事件
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
var cityList = new List<Cities>(combobox1.SelectedValue);
combobox2.DataSource = cityList;
combobox2.DisplayMember = "City";
combobox2.ValueMember = "CityId";
}
我希望這能為您提供一個足夠好的例子,隨時提出更多問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.