【发布时间】:2010-09-16 13:25:29
【问题描述】:
干草。 我正在使用属性网格向集合添加或删除对象。 但是当collectioneditor仅在网格刷新后关闭时。 添加另一个对象后,网格不会刷新。 列表中的集合。 我见过很多人有同样的问题,但没有解决方案。 谢谢
【问题讨论】:
标签: c# propertygrid
干草。 我正在使用属性网格向集合添加或删除对象。 但是当collectioneditor仅在网格刷新后关闭时。 添加另一个对象后,网格不会刷新。 列表中的集合。 我见过很多人有同样的问题,但没有解决方案。 谢谢
【问题讨论】:
标签: c# propertygrid
我意识到我参加聚会很晚了,但是就这样吧。我用这个基类
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。集合表单关闭时会自动刷新属性网格。
但请注意,此解决方案正在反映到属性网格的内部,并且可以随时中断,但我已经这样做了一段时间了,没有任何问题
【讨论】:
实现INotifyCollectionChanged接口或使用ObservableCollection类。
见link
【讨论】: