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