Two comboboxes with the same CollectionViewSource ItemSource update each other

徘徊边缘 提交于 2019-12-05 20:03:33

问题


On my ViewModel, I have 2 properties (both implement property changed notifications) :

CountryOfIssue
Nationality

On my View, I have a CollectionViewSource pointing to a local instance of my 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 comboboxes used to set the values of CountryOfIssue and Nationality :

<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 setup, whenever I change one of the comboboxes' value, the other one changes as well... Is this expected behavior?

(I've implemented a fix by using another CollectionViewSource, I just want to know if this is normal)


回答1:


This is normal, CollectionViews have a CurrentItem and if the ItemsSource is a CollectionView they get synchronized, see IsSynchronizedWithCurrentItem:

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

So you can just turn it off manually by setting that property to false.

(By the way, you can also bind to the CurrentItem of a CollectionView via a slash. e.g. People/Name binds to the Name property of the current person in People.)



来源:https://stackoverflow.com/questions/12367249/two-comboboxes-with-the-same-collectionviewsource-itemsource-update-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!