【问题标题】:How can I load data in combo item property when another changed, in propertygrid c#?当另一个更改时,如何在 propertygrid c# 中加载组合项属性中的数据?
【发布时间】:2012-10-04 01:23:51
【问题描述】:

我的班级有两个属性:MyCountryMyCity。我将此类设置为属性网格的源对象。我想在选择一个国家时加载我组合的城市。例如,我有 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 在城市项目中。

我该怎么办?

我的班级是:

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

【问题讨论】:

    标签: c# propertygrid


    【解决方案1】:

    ITypeDescriptorContext 接口提供了一个名为Instance 的属性 这使您可以访问类型描述符请求所连接的对象。

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

    此外,在MyCountry 属性的设置器中,我检查是否 新值与旧值不同,如果是这种情况,我会重置 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 属性的设置器中的代码替换为:

    myCountry = value; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      相关资源
      最近更新 更多