2012-07-22 49 views
0

我需要自己從ObservableCollection中移除項目。 是否有可能做這樣的事情?如何創建具有自我移除方法的ObservableCollection項目

public class Item 
{ 
    public Item() 
    { 
     // Constructor 
    } 

    public string ID 
    { 
     get; 
     set; 
    } 

    public void Remove() 
    { 
     // I need to write some code here if it is possible 
    } 
} 

//---------- this part is in a ClickEvent i.e. 

ObservableCollection<Item> Items = new ObservableCollection<Item>(); 

Item _Item = new Item() { ID = 1 } 

Items.Add(_Item); 

_Item.Remove() 

注:我不想使用Items.Remove(_Item)方法。

謝謝...

回答

0

所有你需要的是每個項目有它的主人ObserableCollection

然後參考,你可以做

void Remove() 
{ 
    Owner.Remove(this); 
} 

void AddToCollection(ObserableCollection<Item> list) 
{ 
    Owner = list; 
    Owner.Add(this); 
} 

你會還必須處理項目添加多個收集的情況。

+0

感謝您的快速響應。 項目集合由DataContract解串器動態填充。在這種情況下,我不知道如何在物品中引用物品。 – kizanlik 2012-07-22 14:53:34

+0

您可以有一個[OnDeserializedAttribute](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx)方法來設置所有者 – 2012-07-22 14:55:50

+0

謝謝。那是我需要的。 – kizanlik 2012-07-22 15:00:54

相關問題