Two combobox with the same CollectionViewSource ItemSource update each other

In my ViewModel, I have 2 properties (both properties of the property changed changed):

CountryOfIssue Nationality 

In my view, I have a CollectionViewSource pointing to a local instance of the Entity Framework context:

 <CollectionViewSource x:Key="cvsCountries" Source="{Binding LocalContext.Countries}" CollectionViewType="{x:Type ListCollectionView}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Name" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource> 

Also on this page, I have two combinations used to set the values โ€‹โ€‹of CountryOfIssue and National:

 <ComboBox IsEnabled="{Binding CanEditCountryOfIssue}" ItemsSource="{Binding Source={StaticResource cvsCountries}}" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedItem="{Binding CountryOfIssue, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}" /> <ComboBox IsEnabled="{Binding CanEditNationality}" ItemsSource="{Binding Source={StaticResource cvsCountries}}" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedItem="{Binding Nationality, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}" /> 

With this setting, when I change one of the comboboxes values, the other changes ... Is this the expected behavior?

(I implemented the fix using another CollectionViewSource, I just want to know if this is normal)

+7
source share
1 answer

This is normal, CollectionViews has a CurrentItem , and if ItemsSource is a CollectionView , they are synchronized, see IsSynchronizedWithCurrentItem :

true if SelectedItem is always in sync with the current item in ItemCollection ; false if SelectedItem is never synchronized with the current item; null if SelectedItem is synchronized with the current item only if Selector uses CollectionView . The default value is null .

So, you can simply disable it manually by setting this property to false .

(By the way, you can also bind to the CurrentItem CollectionView with a slash. For example, People/Name bound to the Name property of the current person in People .)

+10
source

All Articles