代码之家  ›  专栏  ›  技术社区  ›  Andreas Brinck

编辑集合时未调用集合

  •  1
  • Andreas Brinck  · 技术社区  · 14 年前

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

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

    但是,当我尝试使用 CollectionEditor , set

    我也试着把我的 List<SomeType>

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

    但都不是 Add ,或 Remove 当我在 .

    1 回复  |  直到 14 年前
        1
  •  3
  •   Justin Niessner    14 年前

    不会调用您的setter,因为在编辑集合时,实际上是先获取对原始集合的引用,然后再对其进行编辑。

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

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

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