2

我有一个很久以前从某个地方抓取的自定义控件:

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,但它是空的,即使它不是,我认为有收藏项,而不是收藏。

4

2 回答 2

1

最好的办法是使用 System.Collections.ObjectModel 中定义的 ObservableCollection。这将在添加或删除集合时引发事件。这个类是框架的一部分,因此无论现在还是将来都应该可以很好地工作。

如果您希望监视集合中的类型(T),那么该类型必须实现 INotifyPropertyChanged。

于 2014-10-16T17:41:27.493 回答
1
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);
    }
}

可以做到这一点。

于 2014-10-20T16:46:21.760 回答