Wpf 组合框绑定

Wpf 组合框绑定,wpf,binding,combobox,Wpf,Binding,Combobox,我有两个组合框,它们都与同一个源绑定 <ComboBox ItemsSource="{Binding Source={StaticResource UsersViewSource}}" 您可以分别为每个组合框绑定SelectedItem属性 i、 e 这样,当设置每个项目时,它将存储到不同的位置。您只需将属性设置为false(默认为null)属性应设置为false: 如果SelectedItem始终为 已与中的当前项同步 项目集合;如果 SelectedItem从不同步 与当前项目关联;

我有两个组合框,它们都与同一个源绑定

<ComboBox ItemsSource="{Binding Source={StaticResource UsersViewSource}}"

您可以分别为每个组合框绑定SelectedItem属性

i、 e

这样,当设置每个项目时,它将存储到不同的位置。

您只需将属性设置为false(默认为null)

属性应设置为false:

如果SelectedItem始终为 已与中的当前项同步 项目集合;如果 SelectedItem从不同步 与当前项目关联;如果 SelectedItem与 仅当选择器使用 集合视图。默认值为 空

这里有一个示例:

<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>

一
二
三
四
五
我猜(从绑定的名称来看)发生这种情况的原因是您正在绑定到一个
CollectionViewSource
(包装一个集合)。此类是WPF使用的代理,其中包括集合的选定项。显然,如果您在两个组合框之间共享此集合,那么您也在共享所选项目


如果将
ItemsSource
设置为非
CollectionViewSource
,控件将自动将其包装为一个。因此,我的建议是直接绑定到集合,而不是包装在
CollectionViewSource
中,或者创建两个
CollectionViewSource
实例,每个
组合框一个

答案的讽刺之处在于,正如您的引用所解释的那样,
IsSynchronizedWithCurrentItem=“False”
在您引用的代码中是完全不必要的,因为您没有绑定到
CollectionView
。如果去掉这些属性,组合框仍然没有链接。我同意这可能不是最好的示例,但将值设置为True,它们会像绑定到CollectionView一样进行同步(我的假设)。如果这确实不适用于CollectionView,那么我将删除答案,然后自己学习一些东西。创建一个快速项目并测试IsSynchronizedWithCurrentItem属性是否适用于CollectionView,它是否确实有效。诚然,我的答案并不完全正确,但它确实提供了一个快速的Kaxaml示例;)
<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>