WPF Combobox绑定

iLe*_*ing 8 wpf binding combobox

我有两个Comboboxes,它们都绑定了相同的Source.

<ComboBox ItemsSource="{Binding Source={StaticResource UsersViewSource}}"
Run Code Online (Sandbox Code Playgroud)

当我在第一个中改变某些东西时,它也反映在第二个中.我不知道如何使用相同的ItemsSource分别保留他们的SelectedItem值.

Met*_*urf 10

IsSynchronizedWithCurrentItem属性应设置为False:

如果SelectedItem始终与ItemCollection中的当前项同步,则返回true;否则返回true.如果SelectedItem从未与当前项同步,则返回false;否则返回false.如果SelectedItem仅在Selector使用CollectionView时与当前项同步,则返回null.默认值为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>
Run Code Online (Sandbox Code Playgroud)

  • 具有讽刺意味的是,正如你的引言所解释的那样,引用代码中的`IsSynchronizedWithCurrentItem ="False"是完全没必要的,因为你没有绑定到`CollectionView`.如果你取出这些属性,组合框仍然没有链接. (3认同)