2012-11-18 18 views
8

2 öğe içeren bir özellik ızgaram var. Ülke & Şehirler. Veritabanında 1 tablo var: LocationId, Title, ParentId öğesini kaydeden CountryCityTable. Ülkeler için parentId = 0 ve şehirler için countryid.WinG biçiminde başka bir öğe değiştiğinde propertygrid öğesi değerlerini nasıl güncelleyebilirim?

Mülk kılavuzumda bunları kullanıyorum ve 2 combobox öğesinde gösteriyorum. kodumu bakınız: Kullanıcı propertygrid ülke unvanını değişti

namespace ProGrid 
{ 
    public class KeywordProperties 
    { 
     [TypeConverter(typeof(CountryLocationConvertor))] 
     public string CountryNames { get; set; } 

     [TypeConverter(typeof(CityLocationConvertor))] 
     public string CityNames { get; set; } 
    } 
} 

Ve

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

     public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
     {    
      HumanRoles Db = new HumanRoles(); 
      List<LocationsFieldSet> Items = new List<LocationsFieldSet>(); 
      Items = Db.LoadLocations(0,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;//false : If you want the user to be able to type in a value that is not in the drop-down list. 
     } 
    } 

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

     public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
     { 
      HumanRoles Db = new HumanRoles(); 
      List<LocationsFieldSet> Items = new List<LocationsFieldSet>(); 
      Items = Db.LoadLocations(1,20); 
      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; 
     } 
    } 
} 

Ve

KeywordProperties Kp = new KeywordProperties(); 
myPropertyGrid.SelectedObject = Kp; 

Şimdi, istediğim, güncellenmiş şehirler listesi (sadece şehirler gösterdiğini parentid Bunlar = countryid). Seçilen ülke kimliğine;

Ayrıca benim sınıfımda nasıl benim kodunda Sayı 20 (Db.LoadLocations (1,20)) değiştirebilirim?

Teşekkür ederiz.

+0

[RefreshProperties] özniteliğini uygulayın. –

+0

Lütfen kodumu bu özellik ile değiştirin. Teşekkürler. –

+0

Hans dediği gibi, bu özellik güzel çalışıyor ve çok temiz. Ayrıca daha fazla ayrıntı için kontrol edin http://stackoverflow.com/a/4955653/586754 –

cevap

3

diğer kelime INotifyPropertyChanged

Microsoft INotifyPropertyChange Documentation

benzer bir şey uygulamak gerekir, size bir mülk şans bazı olay ortaya olması gerekir. Hatırladığım kadarıyla özellik ızgarası, o olay/arayüz türünü otomatik olarak kontrol eder ve olay yükseltildiğinde doğru özellik düğümünü yeniler.

önemli bir parçasıdır:

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

ve

public bool MyBoolProperty 
{ 
    get { return myBoolField; } 
    set 
    { 
     myBoolField = value; 
     NotifyPropertyChanged(); 
    } 
} 

Eğer PropertyGrid kapsamında değildir bir şey yapmak istiyorsanız, basitçe PropertyChanged olay kendi yöntemi kaydetmeniz gerekir ve ne istersen onu yap.

+0

Lütfen kodumu bu öznitelikle değiştirin. Teşekkürler. –

İlgili konular