我正在构建一系列winforms应用程序,并且我需要在选择国家/地区城市列表时填写具有2个组合“国家和城市”的propertyGrid。
我一直在寻找一个例子,却找不到任何例子。
任何地方的任何链接或codenippet?
非常感谢答案 0 :(得分:3)
你需要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 :(得分:0)
创建一个具有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";
}
我希望这能为你提供一个足够好的榜样,随时提出更多问题