Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Combobox binding

I got two Comboboxes and both of them have binding with the same Source.

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

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.

like image 834
iLemming Avatar asked Mar 16 '10 21:03

iLemming


2 Answers

The IsSynchronizedWithCurrentItem property should be set to False:

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.

Here's a sample:

<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>
like image 113
Metro Smurf Avatar answered Dec 26 '22 09:12

Metro Smurf


You just need to set the IsSynchronizedWithCurrentItem property to false (by default it's null)

like image 30
Thomas Levesque Avatar answered Dec 26 '22 08:12

Thomas Levesque