views:

584

answers:

4

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.

A: 

You can separately bind the SelectedItem property for each combo box separately.

i.e.

SelectedItem={Binding SelectedItem1}

This way when each one's item gets set, it gets stored into a different place.

Chris
+1  A: 

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

Thomas Levesque
+2  A: 

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"&gt;
   <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>
Metro Smurf
The irony of your answer is that - as your quote explains - the `IsSynchronizedWithCurrentItem="False"` in your quoted code is completely unnecessary, because you're not binding to a `CollectionView`. If you take those attributes out, the comboboxes still aren't linked.
Dan Puzey
Agreed that it may not be the best example, but set the value to True and they do synchronize as if they were bound to a CollectionView (my assumption). If indeed this does not work with a CollectionView, then be all means I shall delete the answer and walk away learning something myself.
Metro Smurf
Threw together a quick project and tested to see if the IsSynchronizedWithCurrentItem property will work with a CollectionView and it does indeed work. Admittedly my answer isn't exactly correct, but it does provide a quick Kaxaml example ;)
Metro Smurf
+1  A: 

I'd guess (from the name of your binding) that the reason this is happening is that you're binding to a CollectionViewSource (that wraps a collection). This class is a proxy that WPF uses that includes (amongst other things) the selected item of a collection. Obviously if you're sharing this collection between two comboboxes, you're also sharing the selected item.

If you set ItemsSource to something that's not a CollectionViewSource, the control will automatically wrap it in one. So, my suggestion would be to bind directly to a collection instead of wrapping in a CollectionViewSource - or, alternatively, create two CollectionViewSource instances, one for each ComboBox.

Dan Puzey