我有一个自定义控件,我很久以前就从这里从某人那里获得了:
public class NotifyingCollectionEditor : CollectionEditor
{
// Define a static event to expose the inner PropertyGrid's PropertyValueChanged event args...
public delegate void MyPropertyValueChangedEventHandler(object sender, PropertyValueChangedEventArgs e);
public static event MyPropertyValueChangedEventHandler ElementChanged;
// Inherit the default constructor from the standard Collection Editor...
public NotifyingCollectionEditor(Type type) : base(type) { }
// Override this method in order to access the containing user controls from the default Collection Editor form or to add new ones...
protected override CollectionForm CreateCollectionForm()
{
// Getting the default layout of the Collection Editor...
CollectionForm collectionForm = base.CreateCollectionForm();
Form frmCollectionEditorForm = collectionForm as Form;
TableLayoutPanel tlpLayout = frmCollectionEditorForm.Controls[0] as TableLayoutPanel;
if (tlpLayout != null)
{
// Get a reference to the inner PropertyGrid and hook an event handler to it.
if (tlpLayout.Controls[5] is PropertyGrid)
{
PropertyGrid propertyGrid = tlpLayout.Controls[5] as PropertyGrid;
propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(propertyGrid_PropertyValueChanged);
}
}
return collectionForm;
}
void propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
// Fire our customized collection event...
var evt = NotifyingCollectionEditor.ElementChanged;
if (evt != null)
evt(this, e);
}
}
当集合中的一个编辑项目发生更改时,它会触发事件,但即使在将某些项目添加或删除到该集合中时,我也需要它来触发。
目前,我并没有其他的想法,只是比较创建表单时和接近表单时元素的数量。
但是,我如何访问编辑后的集合以获得它的Count
值呢?
我试图访问propertyGrid.SelectedObject
,但它为null,即使不是,我也认为存在集合项,而不是集合。
最好使用在System.Collections.ObjectModel中定义的ObservableCollection。当将集合添加到或从中删除时,这将引发事件。这个类是框架的一部分,因此无论是现在还是将来都应该工作得很好。
如果希望监视集合中的类型(T),则该类型必须实现INotifyPropertyChanged。
public class NotifyingCollectionEditor : CollectionEditor
{
// Define a static event to expose the inner PropertyGrid's PropertyValueChanged event args...
public static event EventHandler<PropertyValueChangedEventArgs> ElementChanged;
// Inherit the default constructor from the standard Collection Editor...
public NotifyingCollectionEditor(Type type) : base(type) { }
// Override this method in order to access the containing user controls from the default Collection Editor form or to add new ones...
protected override CollectionForm CreateCollectionForm()
{
// Getting the default layout of the Collection Editor...
CollectionForm collectionForm = base.CreateCollectionForm();
Form frmCollectionEditorForm = collectionForm as Form;
TableLayoutPanel tlpLayout = frmCollectionEditorForm.Controls[0] as TableLayoutPanel;
if (tlpLayout != null)
{
// Get a reference to the inner PropertyGrid and hook an event handler to it.
if (tlpLayout.Controls[5] is PropertyGrid)
{
PropertyGrid propertyGrid = tlpLayout.Controls[5] as PropertyGrid;
propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(propertyGrid_PropertyValueChanged);
}
}
return collectionForm;
}
protected override object SetItems(object editValue, object[] value)
{
object ret_val = base.SetItems(editValue, value);
// Fire our customized collection event...
var evt = NotifyingCollectionEditor.ElementChanged;
if (evt != null)
evt(this, null);
return ret_val;
}
void propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
// Fire our customized collection event...
var evt = NotifyingCollectionEditor.ElementChanged;
if (evt != null)
evt(this, e);
}
}
可以做到。