WPF 组合框绑定 [英] WPF Combobox binding
问题描述
我有两个组合框,它们都绑定了同一个源.
I got two Comboboxes and both of them have binding with the same Source.
<ComboBox ItemsSource="{Binding Source={StaticResource UsersViewSource}}"
当我在第一个中更改某些内容时,它也会反映到第二个中.而且我不知道如何使用相同的 ItemsSource 分别保留它们的 SelectedItem 值.
And when I change something in the first one, it reflects also to the second one. And I dunno how to keep their SelectedItem values separately, using the same ItemsSource.
推荐答案
IsSynchronizedWithCurrentItem 属性应设置为 False:
The IsSynchronizedWithCurrentItem property should be set to False:
如果 SelectedItem 总是与当前项目同步项目集合;假如果SelectedItem 永远不会同步与当前项目;null 如果SelectedItem 与current item only if the Selector uses一个集合视图.默认值为空.
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.
这是一个示例:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Array x:Key="myStrings" Type="sys:String">
<sys:String>one</sys:String>
<sys:String>two</sys:String>
<sys:String>three</sys:String>
<sys:String>four</sys:String>
<sys:String>five</sys:String>
</x:Array>
</Page.Resources>
<StackPanel Width="200">
<ComboBox IsSynchronizedWithCurrentItem="False" Margin="25"
ItemsSource="{Binding Source={StaticResource myStrings}}" />
<ComboBox IsSynchronizedWithCurrentItem="False" Margin="25"
ItemsSource="{Binding Source={StaticResource myStrings}}" />
</StackPanel>
</Page>
这篇关于WPF 组合框绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!