簡體   English   中英

在Winform C#中更改另一個項目時,如何更新PropertyGrid項目值?

[英]How can I update propertygrid item values when another item changed in winform c#?

我有一個包含2個項目的屬性網格。 國家和城市。 我在數據庫中有1個表:CountryCityTable,用於保存LocationId,Title和ParentId。 對於國家/地區,parentId = 0,對於城市為countryid。

在我的propertygrid中,我使用它們並在2個組合框項目中顯示。 請看我的代碼:

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

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

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

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

現在,我想當用戶在propertygrid中更改國家/地區標題時,更新城市列表(只顯示其父代= countryid的城市)。

另外,在我的班級中,如何將代碼( Db.LoadLocations(1,20); )中的數字20更改為所選的國家/地區ID?

謝謝。

您將需要實現類似於INotifyPropertyChanged東西

Microsoft INotifyPropertyChange文檔

換句話說,當您有機會擁有一個物業時,您將需要引發一些事件。 據我所記得,屬性網格會自動檢查事件/接口的類型,並在引發事件時刷新正確的屬性節點。

重要的部分是:

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

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

如果您想做PropertyGrid沒涵蓋的事情,您只需要在PropertyChanged事件中注冊自己的方法並做任何您想做的事情即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM