c# - 在 propertygrid c# 中,当另一个更改时,如何在组合项属性中加载数据?

标签 c# propertygrid

我的类(class)有两个属性:MyCountry & MyCity。我将此类设置为属性网格的 sourceobject。我想在选择国家时加载我组合的城市。例如我有 2 个国家/地区数据:

Country1
Country2

对于 Country1,我有(城市数据)

City11
City12
City13

对于 Country2,我有(城市数据)

city21
City22
City23

当我在 propertygrid 中更改选择国家项目时,我想在城市项目中加载它的城市。 this mean, when select Country1, display City11,City12,City13 in City item and when select Country2 Display City21,Cityy22,City23 在城市项目中。

我该怎么办?

我的类(class)是:

public class KeywordProperties
{
    [TypeConverter(typeof(CountryLocationConvertor))]
    public string MyCountry { get; set; }
    [TypeConverter(typeof(CityLocationConvertor))]
    public string MyCity { get; set; }
}

我使用下面的类加载国家/地区数据以组合显示:

public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {            
        HumanRoles Db = new HumanRoles();
        List<LocationsFieldSet> Items = new List<LocationsFieldSet>();
        Items = Db.LoadLocations(0);
        string[] LocationItems = new string[Items.Count];
        int count = 0;
        foreach (LocationsFieldSet Item in Items)
        {
            LocationItems[count] = Item.Title;
            count++;
        }
        return new StandardValuesCollection(LocationItems);
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;        
    }
}

最佳答案

ITypeDescriptorContext 接口(interface)提供了一个名为Instance 的属性 它允许您访问类型描述符请求连接到的对象。

您可以使用此属性来确定 MyCountry 属性的当前值 用户选择。根据该值,您可以加载该国家/地区的城市。

此外,在 MyCountry 属性的 setter 中,我检查是否 新值与旧值不同,如果是这种情况,我将重置 MyCity 属性 (不要得到无效的国家和城市组合)。

这是一个小代码示例。为了简单起见,我只使用一种类型的转换器 对于这两个属性。

public class KeywordProperties
{    
  public KeywordProperties()
  {
    MyCountry = "Country1";
  }

  private string myCountry;
  [TypeConverter(typeof(ObjectNameConverter))]
  public string MyCountry
  {
    get { return myCountry; }
    set 
    {
      if (value != myCountry)
        MyCity = "";

      myCountry = value; 
    }
  }

  private string myCity;
  [TypeConverter(typeof(ObjectNameConverter))]
  public string MyCity
  {
    get { return myCity; }
    set { myCity = value; }
  }   
}

public class ObjectNameConverter : StringConverter
{
  public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  {
    return true;
  }

  public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  {
    KeywordProperties myKeywordProps = context.Instance as KeywordProperties;

    if (context.PropertyDescriptor.Name == "MyCountry")
    {
      List<string> listOfCountries = new List<string>();
      listOfCountries.Add("Country1");
      listOfCountries.Add("Country2");        

      return new StandardValuesCollection(listOfCountries);
    }      

    List<string> listOfCities = new List<string>();
    if (myKeywordProps.MyCountry == "Country1")
    {
      listOfCities.Add("City11");
      listOfCities.Add("City12");
      listOfCities.Add("City13");
    }
    else
    {
      listOfCities.Add("City21");
      listOfCities.Add("City22");
      listOfCities.Add("City23");
    }

    return new StandardValuesCollection(listOfCities);
  }
}

在上面的例子中有一个副作用我不喜欢。 设置 MyCountry 属性会导致同时设置 MyCity 属性。

要解决此副作用,您还可以使用 PropertyValueChanged 事件 PropertyGrid 处理无效的国家/城市选择。

private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
  if (e.ChangedItem.Label == "MyCountry")
  {
    if(e.ChangedItem.Value != e.OldValue)
      m.MyCity = "";
  }
}

如果您使用此事件,只需将 MyCountry 属性的 setter 中的代码替换为:

myCountry = value; 

关于c# - 在 propertygrid c# 中,当另一个更改时,如何在组合项属性中加载数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706892/

相关文章:

c# - 如何更改 PropertyGrid 控件的边框颜色(或删除边框)?

c# - NHibernate 高效删除使用 LINQ Where 条件

c# - 从范围 '' 引用类型为 '' 的变量 '',但未定义

c# - 有没有办法将PropertyGrid控件的字符串字段标记为 "No nulls or empty strings allowed"?

.net - 如何强制对系统类型使用自定义 UITypeEditor

.net - Propertygrid UIEditor 通过键盘禁用值编辑

c# - 为什么 Enumerable.Except 返回不同的项目?

c# - 无法将不可为 null 的对象作为接受 nullalble 的参数传递

c# - 如何使用自定义委托(delegate)对没有参数的方法进行 stub ?

vb.net - Propertygrid 中的复选框列表