0

I look for a way to observe a collection for changes, without using indices. I know when items are added and removed from the source collection, but the collection is not using indices (it's a custom kind of hashset with notification on add/remove). Items are being populated to the list in a undeterministic order and using indices wouldnt make much sense, so I'm trying to avoid it completely. But Im still going to bind this list to a view, so there will be some ordering of the items eventually. My goal is to have all the ordering in a collectionview.

The question is if there is a way to make a collectionview on a index-less source collection and still get the UI to respond to items being removed and added effectively without having to rebuild the list everytime. I'm not sure if I make any sense here. My goal is to get rid of indices but still benefit from collectionchanged-events and collectionview-ordering. Possible?

UPDATE

I've tried to implement a custom ICollectionView such as SetCollectionView(HashSet set) but it won't work for some reason. Not yet anyway.

Another option could perhaps be to implement a custom ReadOnlyObservableCollection-wrapper with some custom ordering on the GetEnumerator. I haven't tested it yet. I would have to sort the list according to the choosen ordering before extracting the index for the NotifyCollectionChanged-event but that should work.

Andreas Zita
  • 7,232
  • 6
  • 54
  • 115
  • 1
    What defines your order if not an index? A hashset is an explicitly un-ordered data structure. – Chris Shain Jan 09 '12 at 15:55
  • 1
    if a HashSet is un-ordered then how do you expect to know which item(s) are to be removed Andreas..? and why would you want an Index-Less custom collection anyway..?? – MethodMan Jan 09 '12 at 15:58
  • In my case I'm not very interested in the position of items, only if they are part of a collection/set or not. The ordering is up to the presentation and view layer. When removing items I'm not using RemoveAt(0) but Remove(item). – Andreas Zita Jan 09 '12 at 16:14

2 Answers2

0

You need to have an index somewhere, because all of the UI binding plumbing is index-based. You can layer an indexed list over your existing hashset, but it's going to be slow (I can't provide a formal proof, but my gut tells me it would be quite awful, something like O(n)). If you want a quick base collection that you can layer re-ordered UI lists on top of, you might want to look into a balanced sorted tree, rather than a hashset.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • The items in the source collection is not by default comparable to each other, so a sorted tree is not of much use here I guess... – Andreas Zita Jan 10 '12 at 09:16
0

You can use the ObservableHashSet class.

See the question here

How can I make an Observable Hashset in C#?

or go directly to the code here:

http://geoffcox.bellacode.com/2011/12/09/observablehashset/

Community
  • 1
  • 1
Geoff Cox
  • 6,102
  • 2
  • 27
  • 30