具有相同CollectionViewSource ItemSource的两个组合框相互更新 [英] Two comboboxes with the same CollectionViewSource ItemSource update each other

查看:144
本文介绍了具有相同CollectionViewSource ItemSource的两个组合框相互更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

CountryOfIssue
Nationality

在我的视图中,我有一个CollectionViewSource指向我的实体框架上下文的本地实例:

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>

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

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?

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

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

推荐答案

这是正常的, CollectionViews 有一个 CurrentItem 如果 ItemsSource 是一个 CollectionView ,则它们会同步,请参阅 IsSynchronizedWithCurrentItem

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


true 如果 SelectedItem 始终与 ItemCollection ; false ,如果 SelectedItem 从不与当前项同步; null ,如果 SelectedItem 仅当选择器使用时与当前项同步 CollectionView 。默认值为 null

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.

所以你可以手动关闭它,将该属性设置为 false

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

(顺便说一句,你也可以绑定到 CurrentItem 一个 CollectionView 通过斜线例如人物/名称绑定到 People 中的当前人物的名称。)

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

这篇关于具有相同CollectionViewSource ItemSource的两个组合框相互更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆