刷新propertygrid中的集合

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

实现INotifyCollectionChanged接口或使用ObservableCollection类。 看到链接

我意识到我参加派对的时间已经很晚了,但现在就去了。 我用这个基类

 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。 收集表单关闭时,它将自动刷新属性网格。

请注意,这个解决方案反映在属性网格的内部,可以随时打破,但我现在已经做了一段时间没有问题