I am trying to implement some combo box sorting using CollectionViewSource. This combo box is actually part of a data template and is repeated in a list view. My first approach seemed to work (using CollectionViewSource) but all my combo boxes shared the same data context. This made it so whenever one of the other boxes was changed all the others changed to reflect - not a desired side effect.
I decided to just pull back and try to implement a basic combo box (not inside a data template) using inline xaml for specifying the CollectionViewSource (as opposed to creating the cvs as a static resource). I have not been able to successfully get my data to display. I'm probably going about this entirely wrong as I'm still new to WPF.
Here is the xaml for my combo box:
<ComboBox>
<ComboBox.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Path=Configurations}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="AgencyName" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Binding.Source>
</Binding>
</ComboBox.ItemsSource>
</ComboBox>
The DataContext of the user control where this combo box lives is bound to an object which has an ObservableCollection called Configurations and each configuration has a property called AgencyName. I've verified that this works fine using standard binding without the cvs so I know everything is fine in that accord.
Any help would be greatly appreciated as I have ran out of excuses to my boss :). I also don't want to have to drop down into code and do the sorting in the code behind (which I could when I build the ObservableCollection but IMHO that violates the DRY principle).
What exactly do you mean by "whenever one of the other boxes was changed all the others changed to reflect"? Are you talking about the SelectedItem? If so, then it may help setting
IsSynchronizedWithCurrentItem = false
in your ComboBox.Beside that: I think as long as you create and sort your ICollectionView in the code behind only once, there is no violation of the DRY principle, because what you do more there is not needed in the XAML anymore. But I see there may be other reasons to say that a feature like sorting should be done in the View, speaking in terms of Model-View-ViewModel.