Using HashSets with ObservableCollection with WPF?

When you create a new ObservableCollection with another collection you are not wrapping that collection, you create a new one where all items of the passed collection are copied to the ObservableCollection. If you want to use an ObservableCollection for the sole purpose of DataBinding, look no further, you can bind to any IEnumerable in WPF. This unfortuantely has the drawback that WPF will not always correctly pickup changes to the bound collection.

If this is an issue you'd probably have to create your own obeservable hashset: public class ObservableHashSet : ObservableCollection { protected override void InsertItem(int index, T item) { if (Contains(item)) { throw new ItemExistsException(item); } base. InsertItem(index, item); } protected override void SetItem(int index, T item) { int I = IndexOf(item); if (i >= 0 && I! = index) { throw new ItemExistsException(item); } base.

SetItem(index, item); } } EDIT: AS already has been pointed out, you can not inherit from HashSet to implement INotifyCollectionChanged. However if you look at the code (using Reflector) for the HashSet class it is pretty simple it should be too hard to mimic that functionality yourself.

Sure you can bind directly to the hashset, but if an item is added WPF won't notice. – Cameron MacFarland Nov 24 '09 at 23:12 1 +1 for the explanation. However, regarding the proposed solution, I would rather inherit from HashSet and implement INotifyCollectionChanged... – Thomas Levesque Nov 24 '09 at 23:16 1 +1 to what Thomas said, except that you can't inherit from HashSet and also implement INotifyCollectionChanged, since HashSet methods aren't virtual and cannot be overridden.

You'll have to implement ICollection, and your implementation would have to wrap HashSet and raise notifications on all mutating methods. – Pavel Minaev Nov 24 '09 at 23:19 @Pavel : good point... I must confess that I didn't check whether Hashset methods were virtual ;) – Thomas Levesque Nov 24 '09 at 23:32.

As bitbonk said, ObservableCollection doesn't wrap the Hashset but copies its elements instead. If you want an Observable Hashset check out How can I make an Observable Hashset in C#?

It seems to me that an Observable(Hash)Set is something that should already be in the . NET framework. – Alex Marshall Nov 25 '09 at 1:23 Sure, at Microsoft Connect.

Connect.microsoft. Com/VisualStudio – Cameron MacFarland Nov 25 '09 at 6:58.

When you create a new ObservableCollection with another collection you are not wrapping that collection, you create a new one where all items of the passed collection are copied to the ObservableCollection. If you want to use an ObservableCollection for the sole purpose of DataBinding, look no further, you can bind to any IEnumerable in WPF. This unfortuantely has the drawback that WPF will not always correctly pickup changes to the bound collection.

AS already has been pointed out, you can not inherit from HashSet to implement INotifyCollectionChanged. However if you look at the code (using Reflector) for the HashSet class it is pretty simple it should be too hard to mimic that functionality yourself.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions