4

I will have a 7 small "shopping lists" and then one big containing all the items from the 7 small ones.

Is it possible to use databind and observablecollection so whatever is added/removed/changed from the small lists are updated in the big list?

public ObservableCollection<ShopItem> MondayShopList{ get; set; }
public ObservableCollection<ShopItem> TuesdayShopList{ get; set; }
public ObservableCollection<ShopItem> WedensdayShopList{ get; set; }
public ObservableCollection<ShopItem> ....
public ObservableCollection<ShopItem> TotalShopList { get; set; }
Mech0z
  • 3,627
  • 6
  • 49
  • 85

2 Answers2

3

i see 3 alternatives for you...

  1. implement a composite ObservableCollection. if there isn't such a thing out there, you can build one by implementing INotifyCollectionChanged
  2. load all items into the total list and respond yourself to the collection changed events of the dayweek collections and make updates to the complete collection
  3. perform a small architectural change and attach weekday information to the ShopItem. now you can keep them all in one list and display filtered sections of the big list.
flq
  • 22,247
  • 8
  • 55
  • 77
  • http://pastebin.com/m28XgTvj how would you in that list only show those with ShopItem.Day == 1 – Mech0z Jan 07 '12 at 12:15
  • I haven't tested, but explore the functionality surrounding CollectionView and CollectionViewSource, which gives you filtering capabilities, etc. See also http://stackoverflow.com/questions/2303376/how-to-filter-observable-collection-class-collection – flq Jan 07 '12 at 15:17
0

No it's not possible to databind nested collections, if not by using WPF TreeView control that supports hierarchical data binding. If you want to show the content of child collections, bind them separately to Ui. The fact that they are childs of another is the matter of code-behind and not necessary should reflect its exact relationship rapresentation on Ui.

For concrete help with implementations of those scenarios look on this MSDN article

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • That's not true, you can use the `TreeView` control to represent a collection of collections. – myermian Jan 07 '12 at 13:37
  • @m-y: you create tough constrain to use *one specific* control to resolve this problem. The question is general. The general solution for this, is to *slice* your data model between different controls, were `TreeView` control is able to host hierarchical data by itself is a kind of special case. – Tigran Jan 07 '12 at 13:43
  • 1
    I was just commenting on the sentence in your answer: "No it's not possible to databind nested collections." I wasn't stating that the OP should go this route, just stating that it *is* to represent a collection of collections in the UI. – myermian Jan 07 '12 at 13:50