DeM*_*ama 6 c# wpf binding combobox itemssource
我有2个ComboBoxes,其ItemsSource被绑定到相同的ObservableCollection:
public ObservableCollection<MyType> MyTypeCollection { get; set; }
Run Code Online (Sandbox Code Playgroud)
我已经定义了2个不同的SelectedItem属性,这些属性又被绑定到正确的属性ComboBox.
public MyType MySelectedItem1 { get; set; }
public MyType MySelectedItem2 { get; set; }
Run Code Online (Sandbox Code Playgroud)
每当我在其中一个中选择一个项目时,我班级中定义的ComboBoxes两个SelectedItem属性都被设置为该选择.两者ComboBoxes也在视觉上设置为该值.
为什么是这样?
我尝试了几样的东西Mode=OneWay,但它没有用.
这是我的XAML(为问题修剪):
<ComboBox SelectedItem="{Binding MySelectedItem1}"
ItemsSource="{Binding MyTypeCollection, Mode=OneWay}"/>
<ComboBox SelectedItem="{Binding MySelectedItem2}"
ItemsSource="{Binding MyTypeCollection, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)
Mik*_*bel 11
我猜你有IsSynchronizedWithCurrentItem="True"两个ComboBox控件.从两者中删除它,这应该可以解决您的问题.
设置IsSynchronizedWithCurrentItem="True"告诉ComboBox它应该保持SelectedItem与CurrentItem底层的同步ICollectionView.由于您直接绑定到集合而不是集合视图,因此两个组合框都使用默认集合视图,这是跨控件共享的常见实例.当您的某个组合框更新集合视图的选择时,另一个组合框会看到更改并更新自己的选择以匹配它.