У меня есть этот пользовательский элемент управления, который я схватил из somewehere давно:Как сделать CollectionEditor для запуска события CollectionChanged, когда элементы добавлены или удалены?
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
, но это не так, и даже если это не так, я думаю, что есть коллекции, а не коллекция.