I'm playing with ICollectionView right now, and am encountering a problem where I think I understand the "why", but not the "how do I fix it". :)
I have a ComboBox that's databound to an ICollectionView, and it is initially set with the following code:
NameView = CollectionViewSource.GetDefaultView( names); // names is an IEnumerable<string> that comes from a LINQ query
NameView.CurrentChanged += new EventHandler(NameView_CurrentChanged);
Everything works great until I execute a piece of code that generates a new IEnumerable<string>
and sets NameView again with the same code as above. Once I do this, CurrentItem
is no longer working properly.
I've run into this problem before with ObservableCollection<string>
databound to ComboBoxes, and I get around the "unbinding" problem by using Clear()
and Add()
instead of setting the ObservableCollection<string>
property to a new ObservableCollection<string>
.
My questions include:
1. If I wanted to be able to just set the property to a new collection, can I re-establish databinding with the new collection somehow? If so, how? If not, can you explain the WPFisms behind why this is fundamentally not possible?
2. What's the best way to deal with changes in an ObservableCollection<string>
or ICollectionView
? Is my approach of just Clearing and Adding the only way to do it?