1

我有一个包含我想在属性网格中显示和编辑的集合属性的类:

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

但是,当我尝试使用 编辑此集合时CollectionEditorset永远不会调用;为什么会这样,我该如何解决?

我还尝试将我的收藏包含List<SomeType>在我自己的收藏中,如下所述:

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

但是当我Add在.RemoveCollectionEditor

4

1 回答 1

3

您的 setter 没有被调用,因为当您编辑集合时,您实际上是在获取对原始集合的引用,然后对其进行编辑。

使用您的示例代码,这只会调用 getter,然后修改现有集合(从不重置它):

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

var textures.Add(new SomeType());

要调用 setter,您实际上必须为 Property 分配一个新集合:

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

yourClass.Textures = newTextures;
于 2010-11-10T13:48:58.620 回答