繁体   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