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

时间:2012-10-03 10:48:01

标签: c# propertygrid

我班上有两个属性: MyCountry &的 MyCity 即可。我将此类设置为属性网格的sourceobject。我想在选择国家时加载我组合的城市。例如,我有2个国家数据:

Country1
Country2

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

City11
City12
City13

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

city21
City22
City23

当我在propertygrid中更改选择国家/地区项目时,我希望在城市项目中加载城市。这意味着,当选择 Country1 时,在城市项目中显示 City11,City12,City13 ,选择 Country2 时显示 City21,Cityy22,City23 在City Item。

我怎么能?

我的课程是:

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

1 个答案:

答案 0 :(得分:1)

ITypeDescriptorContext接口提供了一个名为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;