两个具有相同CollectionViewSource ItemSource的组合框相互更新

时间:2022-01-31 13:02:57

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

在我的ViewModel上,我有2个属性(都实现了属性更改通知):

CountryOfIssue
Nationality

On my View, I have a CollectionViewSource pointing to a local instance of my Entity Framework context :

在我的View上,我有一个指向我的Entity Framework上下文的本地实例的CollectionViewSource:

<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 :

同样在这个页面上,我有两个组合框用于设置CountryOfIssue和国籍的值:

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

(我已经使用另一个CollectionViewSource实现了修复,我只是想知道这是否正常)

1 个解决方案

#1


10  

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

这是正常的,CollectionViews有一个CurrentItem,如果ItemsSource是一个CollectionView,它们会被同步,请参阅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.

如果SelectedItem始终与ItemCollection中的当前项同步,则返回true;否则返回true。如果SelectedItem从不与当前项同步,则返回false;否则返回false。如果SelectedItem仅在Selector使用CollectionView时与当前项同步,则返回null。默认值为null。

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

因此,您可以通过将该属性设置为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.)

(顺便说一句,您还可以通过斜杠绑定到CollectionView的CurrentItem。例如,People / Name绑定到People中当前人员的Name属性。)

#1


10  

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

这是正常的,CollectionViews有一个CurrentItem,如果ItemsSource是一个CollectionView,它们会被同步,请参阅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.

如果SelectedItem始终与ItemCollection中的当前项同步,则返回true;否则返回true。如果SelectedItem从不与当前项同步,则返回false;否则返回false。如果SelectedItem仅在Selector使用CollectionView时与当前项同步,则返回null。默认值为null。

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

因此,您可以通过将该属性设置为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.)

(顺便说一句,您还可以通过斜杠绑定到CollectionView的CurrentItem。例如,People / Name绑定到People中当前人员的Name属性。)