propertygrid c#4.0中的级联组合框

propertygrid c#4.0中的级联组合框,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我正在构建一系列winforms应用程序,在选择国家/城市列表时,我需要一个带有2个组合“Country and city”的propertyGrid 我一直在寻找一个例子,但没有找到 任何地方有链接或代码片段吗 非常感谢创建一个带有1个静态函数的类country来检索所有已知的国家 class Countries { public string Country{get;set;} public int CountryId {get;set;} public static List<

我正在构建一系列winforms应用程序,在选择国家/城市列表时,我需要一个带有2个组合“Country and city”的propertyGrid

我一直在寻找一个例子,但没有找到

任何地方有链接或代码片段吗


非常感谢

创建一个带有1个静态函数的类country来检索所有已知的国家

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;
 }
}
以加载或其他形式调用下面的函数来加载CountryBox

private void PopulateCountryBox()
{
 var countryList = new List<Countries>();
 combobox1.DataSource = countryList;
 combobox1.DisplayMember = "Country";
 combobox1.ValueMember = "CountryId"; 
}
private void PopulateCountryBox()
{
var countryList=新列表();
combobox1.DataSource=countryList;
combobox1.DisplayMember=“国家/地区”;
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"; 
}
private void组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
var cityList=新列表(combobox1.SelectedValue);
combobox2.DataSource=城市列表;
combobox2.DisplayMember=“城市”;
combobox2.ValueMember=“CityId”;
}

我希望这为您提供了一个足够好的示例,您可以从这里开始,自由地提出更多问题

您需要类型转换器,一个用于城市,一个用于国家:

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);
    }
  }
}

larstech的答案看起来更适合你,因为我的答案是在组合框上工作,而不是在属性上。Grid:这非常感谢你的时间和精力,但larstech确实是你说的我要找的。在从第二个组合中选择值后,我改为第一个组合。正如所料,第二个组合并没有使值无效。
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
 var cityList = new List<Cities>(combobox1.SelectedValue);
 combobox2.DataSource = cityList;
 combobox2.DisplayMember = "City";
 combobox2.ValueMember = "CityId"; 
}
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();