views:

34

answers:

2

I have a custom control that has an Items property. I Have applied an EditorAttribute with a UITypeEditor of type CollectionEditor.

Collection Type:

[Serializable]
[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public class ListItemsCollection : CollectionBase
{
    // methods
}

Property Declaration In The Control:

private new ListItemsCollection _Items;

[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public new ListItemsCollection Items
{
    get
    {
        return _Items;
    }
    set
    {
        _Items = value;

        // do other UI changes
    }
}

Problem:
When I drop this control to the designer surface, I am able to add items to the Items property using the PropertyGrid. But, the when I click the Ok button of the CollectionEditor the setter of the Items property is not getting called.

AFAIK when a value is returned from the EditValue method of a UITypeEditor class the setter block of the property is supposed to be called.

This is driving me insane. I even tried adding Event's to the ListItemsCollection, so that when Items are added, I can whatever I want with the control's ui.

This is not supposed to be hard. What am I doing wrong?

+1  A: 

Collection properties should be read-only. It's the collection that is retrieved through the getter, and adjusted. The setter never enters into it, because that would mean setting a new collection.

Joachim VR
How do I do certain operation when the collection is modified using the CollectionEditor?
Zuhaib
You could only know if your collection itself has any events, which is not often the case, and definitely not in a ListItemCollection. You could try and write your own collection, or inherit from ListItemCollection, and override the add, delete, insert methods to invoke an event you defined yourself, after being called.
Joachim VR
+1  A: 

I try to reprodeuce your situation: using following code, I get a message box showing whenever I edit the list from VS property window. Beware that you have to create the list by yourself. If you don't create it, VS create a temp list which you can edit from property window, but does not set your property to this list (so your setter will never be called)

    public UserControl1()
    {
        InitializeComponent();
        list = new BindingList<ListViewItem>();
        list.ListChanged += new ListChangedEventHandler(list_ListChanged);
    }

    void list_ListChanged(object sender, ListChangedEventArgs e)
    {
        MessageBox.Show(e.ListChangedType.ToString());
    }

    private BindingList<ListViewItem> list;

    public BindingList<ListViewItem> List1
    {
        get { return list; }
    }
Andrea Parodi
Zuhaib