当Winform C#中的另一个项更改时,我该如何更新PropertyGrid项目值?

8

我有一个属性网格,包含2个项目:国家和城市。我在数据库中有一张表:CountryCityTable,保存位置ID、标题和父级ID。对于国家,父级ID为0;对于城市,父级ID是所属的国家ID。

在我的属性网格中,我使用这些数据,并显示在2个组合框中。请参见我的代码:

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

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

And

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

And

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

现在,我希望当用户在属性网格中更改国家标题时,城市列表会更新(只显示父ID为国家ID的城市)。

此外,在我的类中,如何将代码中的数字20(Db.LoadLocations(1,20);)更改为所选国家的ID?

谢谢。


应用[RefreshProperties]属性。 - Hans Passant
请使用这个属性更改我的代码。谢谢。 - Ali Ahmadi
就像Hans所说,这个属性非常好用且非常干净。还可以查看更多详细信息:https://dev59.com/WU7Sa4cB1Zd3GeqP5q0k#4955653 - Andreas Reiff
1个回答

3
你需要实现类似于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事件中注册自己的方法,并做任何您想做的事情即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接