Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding with CollectionViewSource

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).

like image 994
SRM Avatar asked Mar 01 '11 20:03

SRM


2 Answers

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.

like image 118
Simon D. Avatar answered Oct 23 '22 01:10

Simon D.


Didn't read your entire post, but the problem is that resources are shared by default. Each combo box was therefore referencing the same collection view. A collection view includes tracking selection, so changing the selection in one combo box would affect the others.

Rather than move the CVS to a local resource, you could just prevent it from being shared:

<CollectionViewSource x:Key="whatever" x:Shared="False" .../>
like image 22
Kent Boogaart Avatar answered Oct 23 '22 02:10

Kent Boogaart