I have a bunch of combos that all share the same available choices. These choices are provided in a collection exposed from my ViewModel. All fine and dandy.
I now want these choices sorted, so I decided to expose an ICollectionView
from my ViewModel instead of my usual ReadonlyObservableCollection<T>
, and sort the collection view in my ViewModel.
class EditStuffViewModel : ViewModelBase
{
public EditStuffViewModel (ObservableCollection<Choice> choices)
{
Choices = new CollectionViewSource() { Source = choices }.View;
Choices.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
}
public ICollectionView Choices
{
get;
private set;
}
//snip other properties
}
This all works fine except that now all my combos now sync their selection.
This is not what I want. I want the choices to be shared, but selections to be to their normal bindings. I think I understand that my CollectionView is tracking selection, but I thought this was behaviour was opt in for each control.
I have tried explicitly setting IsSynchronizedWithCurrentItem="False"
on my combos which successfully decouples them, but then my bound SelectedItem
is never selected in the combo (the ViewModel's bound getter is called but the result is never selected). Selecting an item does seem to update my ViewModel's setter correctly.
I'm obviously missing something fundamental to how CollectionView is supposed to work. Can anyone enlighten me?
EDIT: My bad, this DOES work with IsSynchronizedWithCurrentItem="False"
. See my answer for details.
Cheers.