2010-11-10 49 views
1

當設置不叫我有一個包含在屬性網格我想顯示集合屬性和編輯類:編輯收集

[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當我添加和刪除在CollectionEditor項目被調用。

+0

好問題,突出了一個常見的誤解。 – annakata 2010-11-10 13:54:01

+0

很樂意幫忙;) – 2010-11-10 13:55:09

回答

3

您的二傳手沒有被調用,因爲當您編輯一個集合時,您真的獲得了對原始集合的引用,然後編輯它。

使用您的示例代碼,這隻會叫吸氣,然後修改現有的集合(從來沒有將其復位):

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; 
+0

凹凸不平。這就是爲什麼公開List而不是IEnumerable和AddFoo方法通常被認爲是反模式的原因。 – annakata 2010-11-10 13:53:08

+0

@annakata你能否詳細說明如何在答案中更詳細地做到這一點? – 2010-11-10 13:57:15

+0

@annakata我在這裏添加了一個新問題http://stackoverflow.com/questions/4145324/whats-the-correct-way-to-edit-a-collection-in-a-property-grid – 2010-11-10 14:09:46