2010-11-17 35 views
0

喜新別的更新我有一個自定義的BindingList包含產品具有以下信息插入只有在的BindingList

string ProductID 
int Amount; 

我怎麼會讓它可以做到以下幾點。

ProductsList.Add(new Product("ID1", 10)); 
ProductsList.Add(new Product("ID2", 5)); 
ProductsList.Add(new Product("ID2", 2)); 

列表應然後包含2個產品

ProductID = "ID1" Amount = 10 
ProductID = "ID2" Amount = 7; 

所以workes有點像一個購物車

林看AddingNew事件和覆蓋無效InsertItem(INT指數,T項目)

但我真的需要一點幫助入門

+0

什麼?請張貼一些代碼 – TalentTuner 2010-11-17 14:11:02

+0

不多,真的沒有任何代碼呢。仍在尋找文檔 – gulbaek 2010-11-17 14:16:55

+0

雖然我最好的選擇是保護覆蓋void InsertItem(int index,T item),如果它的新產品調用base.InsertItem(index,item);否則請更新 – gulbaek 2010-11-17 14:20:22

回答

1

我真的不知道爲什麼你需要這個自定義列表,因爲.net庫中有很多好的集合,但我已經嘗試了下面的東西。

public class ProductList 
{ 
    public string ProductID {get;set;} 
    public int Amount {get;set;} 
} 

public class MyBindingList<T>:BindingList<T> where T:ProductList 
{ 

    protected override void InsertItem(int index, T item) 
    { 

     var tempList = Items.Where(x => x.ProductID == item.ProductID); 
     if (tempList.Count() > 0) 
     { 
      T itemTemp = tempList.FirstOrDefault(); 
      itemTemp.Amount += item.Amount; 


     } 
     else 
     { 
      if (index > base.Items.Count) 
      { 
       base.InsertItem(index-1, item); 
      } 
      else 
       base.InsertItem(index, item); 

     } 

    } 

    public void InsertIntoMyList(int index, T item) 
    { 
     InsertItem(index, item); 
    } 



} 

並在客戶端代碼中您可以使用此列表。

 ProductList tempList = new ProductList() { Amount = 10, ProductID = "1" }; 
     ProductList tempList1 = new ProductList() { Amount = 10, ProductID = "1" }; 
     ProductList tempList2 = new ProductList() { Amount = 10, ProductID = "2" }; 
     ProductList tempList3 = new ProductList() { Amount = 10, ProductID = "2" }; 

     MyBindingList<ProductList> mylist = new MyBindingList<ProductList>(); 

     mylist.InsertIntoMyList(0, tempList); 
     mylist.InsertIntoMyList(1, tempList1); 
     mylist.InsertIntoMyList(2, tempList2); 
     mylist.InsertIntoMyList(3, tempList); 
     mylist.InsertIntoMyList(4, tempList1); 
     mylist.InsertIntoMyList(0, tempList3); 
+0

非常感謝您的解決方案,儘管我跳過了InsertInfoMyList並只使用Add方法,但完美地工作。 – gulbaek 2010-11-18 08:20:37

1

創建你自己的集合很少是正確的選擇 - 在這種情況下,我傾向於遏制而不是繼承。喜歡的東西

class ProductsList 
{ 
    private readonly SortedDictionary<string, int> _products 
            = new Dictionary<string,int>(); 

    public void AddProduct(Product product) 
    { 
     int currentAmount; 
     _products.TryGetValue(product.ProductId, out currentAmount); 

     //if the product wasn't found, currentAmount will be 0 
     _products[product.ProductId] = currentAmount + product.Amount; 
    } 
} 

評論:

  • 如果你需要一個實際的IEnumerable(而不是一個字典),用KeyedCollection
  • 而不是創建自己的類(這迫使你實現所有你想要的方法),你可以寫一個IEnumerable<Product>的擴展方法,類似於AddProduct - 但是這不會隱藏常規的Add方法嗎?我猜這是更加快速和骯髒的做法

最後,不要擔心IBindingList部分 - 你可以隨時使用你嘗試過,直到又BindingSource

+0

對不起,我不太清楚,但我需要使用我自己的集合,因爲我需要我的列表是可排序的,並且同時支持INotifyPropertyChanged。我只是刪除了大部分代碼,以使這個問題儘可能簡單。 – gulbaek 2010-11-18 08:23:59

+0

您正在混淆簡單的數據綁定(綁定到對象的屬性) - 需要INotifyPropertyChanged,以及需要IBindingList的複雜數據綁定(綁定到列表)。事實上,如果你看看saurabh的集合,它根本不會實現INotifyPropertyChanged(因爲BindingList 沒有)。就像我說的,通過使用BindingSource,您可以免費獲得IBindingList。關於排序,添加它非常簡單,例如你可以使用SortedDictionary(見上文)。底線是,創建你自己的收藏更容易出錯和多餘 – 2010-11-18 13:05:26