【问题标题】:How can I update propertygrid item values when another item changed in winform c#?当 winform c# 中的另一个项目发生更改时,如何更新 propertygrid 项目值?
【发布时间】:2012-11-18 12:25:39
【问题描述】:

我有一个包含 2 个项目的属性网格。国家和城市。我在数据库中有 1 个表:保存 LocationId、Title、ParentId 的 CountryCityTable。对于国家,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 中更改国家/地区标题时,更新城市列表(仅显示这些的 parentid = countryid 的城市)。

另外,在我的课堂上,我如何将代码中的数字 20(Db.LoadLocations(1,20);) 更改为选定的国家/地区 ID?

谢谢。

【问题讨论】:

  • 应用 [RefreshProperties] 属性。
  • 请使用此属性更改我的代码。谢谢。
  • 正如汉斯所说,这家酒店运作良好,非常干净。另请检查更多详细信息stackoverflow.com/a/4955653/586754

标签: c# propertygrid


【解决方案1】:

你需要实现类似于INotifyPropertyChanged的东西

Microsoft INotifyPropertyChange Documentation

换句话说,当您遇到一个属性时,您将需要引发一些事件。据我记得,属性网格会自动检查该类型的事件/接口,并在引发事件时刷新正确的属性节点。

重要的部分是:

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 事件中注册你自己的方法,然后做任何你想做的事情。

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多