2012-11-18 6 views
8

Ich habe ein Grundstücksraster mit 2 Elementen. Land & Städte. Ich habe 1 Tabelle in der Datenbank: CountryCityTable, die LocationId, Titel, ParentId speichern. Für Länder parentId = 0 und für Städte ist countid.Wie kann ich PropertyGrid-Elementwerte aktualisieren, wenn ein anderes Element in WinForm C# geändert wurde?

In meinem propertygrid verwende ich diese und zeige in 2 combobox items. Bitte beachten Sie meinen Code:

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

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

Und

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

Und

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

Nun, ich möchte, wenn der Benutzer Land Titel in Property geändert, Liste der Städte aktualisiert (nur Städte anzuzeigen, die parentid von diesen = countryid).

Auch in meiner Klasse wie kann ich Nummer 20 in meinem Code (Db.LoadLocations (1,20);) zu ausgewählten Länder-ID ändern?

Danke.

+0

Wenden Sie das Attribut [RefreshProperties] an. –

+0

Bitte ändern Sie meinen Code mit diesem Attribut. Vielen Dank. –

+0

Wie Hans sagte, diese Eigenschaft funktioniert gut und ist sehr sauber. Überprüfen Sie auch für mehr Details http://stackoverflow.com/a/4955653/586754 –

Antwort

3

Sie müssen etwas ähnliches wie INotifyPropertyChanged

Microsoft INotifyPropertyChange Documentation

Mit anderen Wort implementieren, müssen Sie ein Ereignis haben, erhöht, wenn Sie eine Eigenschaft Chance. Soweit ich mich erinnere, überprüft das Eigenschaftenraster automatisch diese Art von Ereignis/Schnittstelle und aktualisiert den korrekten Eigenschaftsknoten, wenn das Ereignis ausgelöst wird.

Der wichtigste Teil ist:

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

und

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

Wenn Sie etwas tun wollen, die nicht von der Property abgedeckt ist, müssen Sie einfach Ihre eigene Methode im PropertyChanged Ereignis registrieren und tu was immer du willst.

+0

Bitte ändern Sie meinen Code mit diesem Attribut. Vielen Dank. –

Verwandte Themen