I have 2 ComboBoxes
whose ItemsSource
is bound to the same ObservableCollection
:
我有2个ComboBox,其ItemsSource绑定到相同的ObservableCollection:
public ObservableCollection<MyType> MyTypeCollection { get; set; }
I have defined 2 different SelectedItem
properties which in turn are bound to the correct ComboBox
.
我已经定义了2个不同的SelectedItem属性,这些属性又绑定到正确的ComboBox。
public MyType MySelectedItem1 { get; set; }
public MyType MySelectedItem2 { get; set; }
Whenever I select an item in one of the ComboBoxes
, both SelectedItem
properties defined in my class are set to that selection. Both ComboBoxes
are also set visually to that value.
每当我在其中一个ComboBox中选择一个项目时,我的类中定义的SelectedItem属性都将设置为该选择。两个ComboBox也可以在视觉上设置为该值。
Why is this?
为什么是这样?
I tried several things like Mode=OneWay
etc, but it's not working.
我尝试了一些像Mode = OneWay等的东西,但它没有用。
Here's my XAML (trimmed down for the question):
这是我的XAML(为问题修剪):
<ComboBox SelectedItem="{Binding MySelectedItem1}"
ItemsSource="{Binding MyTypeCollection, Mode=OneWay}"/>
<ComboBox SelectedItem="{Binding MySelectedItem2}"
ItemsSource="{Binding MyTypeCollection, Mode=OneWay}"/>
1 个解决方案
#1
8
I'm guessing you have IsSynchronizedWithCurrentItem="True"
on both ComboBox
controls. Remove it from both, and that should solve your problem.
我猜你在两个ComboBox控件上都有IsSynchronizedWithCurrentItem =“True”。从两者中删除它,这应该可以解决您的问题。
Setting IsSynchronizedWithCurrentItem="True"
tells the ComboBox
that it should keep its SelectedItem
in sync with the CurrentItem
in the underlying ICollectionView
. Since you are binding directly to a collection, and not a collection view, both combo boxes are using the default collection view, which is a common instance shared across controls. When one of your combo boxes updates the collection view's selection, the other sees the change and updates its own selection to match it.
设置IsSynchronizedWithCurrentItem =“True”告诉ComboBox它应该使其SelectedItem与底层ICollectionView中的CurrentItem保持同步。由于您直接绑定到集合而不是集合视图,因此两个组合框都使用默认集合视图,这是跨控件共享的常见实例。当您的某个组合框更新集合视图的选择时,另一个会看到更改并更新自己的选择以匹配它。
#1
8
I'm guessing you have IsSynchronizedWithCurrentItem="True"
on both ComboBox
controls. Remove it from both, and that should solve your problem.
我猜你在两个ComboBox控件上都有IsSynchronizedWithCurrentItem =“True”。从两者中删除它,这应该可以解决您的问题。
Setting IsSynchronizedWithCurrentItem="True"
tells the ComboBox
that it should keep its SelectedItem
in sync with the CurrentItem
in the underlying ICollectionView
. Since you are binding directly to a collection, and not a collection view, both combo boxes are using the default collection view, which is a common instance shared across controls. When one of your combo boxes updates the collection view's selection, the other sees the change and updates its own selection to match it.
设置IsSynchronizedWithCurrentItem =“True”告诉ComboBox它应该使其SelectedItem与底层ICollectionView中的CurrentItem保持同步。由于您直接绑定到集合而不是集合视图,因此两个组合框都使用默认集合视图,这是跨控件共享的常见实例。当您的某个组合框更新集合视图的选择时,另一个会看到更改并更新自己的选择以匹配它。