set not being called when editing a collection

天涯浪子 提交于 2019-12-11 08:24:34

问题


I have a class containing a collection property which I want to display and edit in a property grid:

[EditorAttribute(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public List<SomeType> Textures
{
    get
    {
        return m_collection;
    }
    set
    {
        m_collection = value;
    }
}

However, when I try to edit this collection with the CollectionEditor, set is never called; why is this and how can I fix it?

I also tried to wrap my List<SomeType> in my own collection as described here:

http://www.codeproject.com/KB/tabs/propertygridcollection.aspx

But neither Add, nor Remove is being called when I add and remove items in the CollectionEditor.


回答1:


Your setter isn't being called because when you're editting a collection, you're really getting a reference to the original collection and then editting it.

Using your example code, this would only call the getter and then modify the existing collection (never resetting it):

var yourClass = new YourClass();
var textures = yourClass.Textures

var textures.Add(new SomeType());

To call the setter, you would actually have to assign a new collection to the Property:

var yourClass = new YourClass();
var newTextures = new List<SomeType>();
var newTextures.Add(new SomeType());

yourClass.Textures = newTextures;


来源:https://stackoverflow.com/questions/4145078/set-not-being-called-when-editing-a-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!