【问题标题】:refresh a collection in propertygrid刷新 propertygrid 中的集合
【发布时间】:2010-09-16 13:25:29
【问题描述】:

干草。 我正在使用属性网格向集合添加或删除对象。 但是当collectioneditor仅在网格刷新后关闭时。 添加另一个对象后,网格不会刷新。 列表中的集合。 我见过很多人有同样的问题,但没有解决方案。 谢谢

【问题讨论】:

    标签: c# propertygrid


    【解决方案1】:

    我意识到我参加聚会很晚了,但是就这样吧。我用这个基类

    public class CollectionEditorBase : CollectionEditor
    {
        protected PropertyGrid ownerGrid;
    
        public CollectionEditorBase(Type type) : base(type) { }
    
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            PropertyInfo ownerGridProperty = provider.GetType().GetProperty("OwnerGrid", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            ownerGrid = (PropertyGrid)ownerGridProperty.GetValue(provider);
    
            return base.EditValue(context, provider, value);
        }
    
        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm cf = base.CreateCollectionForm();
            cf.FormClosing += delegate(object sender, FormClosingEventArgs e)
            {
                ownerGrid.Refresh();
            };
    
            return cf;
        }
    }
    

    然后您只需在此基础上创建一个新的 Collectioneditor。集合表单关闭时会自动刷新属性网格。

    但请注意,此解决方案正在反映到属性网格的内部,并且可以随时中断,但我已经这样做了一段时间了,没有任何问题

    【讨论】:

      【解决方案2】:

      实现INotifyCollectionChanged接口或使用ObservableCollection类。 见link

      【讨论】:

      • 你能给我一个很好的例子吗?
      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多