1
Answer

Compare combobox items

Photo of Avuya Mxoli

Avuya Mxoli

12y
3.5k
1

Hi all,

I am working on a WPF C# app where I have two comboboxes and two listboxes. On load I populate both comboboxes with the same data. What I want to do on selection changed event of the first combobox is to compare the selected text with all the items in the 2nd combobox and when it is found it should be removed from the 2nd combobox because I am trying to avoid selecting the same record on both comboboxes. (The data in the comboboxes comes from the database).

This is the logic of what I want to do:
The comboboxes are loaded with teacher's names. So when I select a teacher on the 1st one, I then choose students on the 1st listbox and move them to the 2nd one where I also select a teacher on the 2nd combobox then I save the teacher in the 2nd combobox with the students in the 2nd listbox because I am exchanging students between these two teachers, that is why I dont want teacher "a" to be re-assigned to the same students because then there will have been no exchange done.

Answers (1)

0
Photo of Nattudurai Eswaramurthy
NA 566 24.5k 12y
HI,


 i thought about it and i would probably approach it with a MultiBinding and a respective ValueConverter, i.e.

<StackPanel>
   
<StackPanel.Resources>
       
<local:ComboBoxItemsSourceFilter x:Key="ComboBoxItemsSourceFilter"/>
   
</StackPanel.Resources>
   
<ComboBox Name="cb1">
       
<ComboBox.ItemsSource>
           
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
               
<Binding Path="Emps"/> <!-- Source collection binding -->
               
<Binding ElementName="cb2" Path="SelectedItem"/>
               
<Binding ElementName="cb3" Path="SelectedItem"/>
           
</MultiBinding>
       
</ComboBox.ItemsSource>
   
</ComboBox>
   
<ComboBox Name="cb2">
       
<ComboBox.ItemsSource>
           
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
               
<Binding Path="Emps"/>
               
<Binding ElementName="cb1" Path="SelectedItem"/>
               
<Binding ElementName="cb3" Path="SelectedItem"/>
           
</MultiBinding>
       
</ComboBox.ItemsSource>
   
</ComboBox>
   
<ComboBox Name="cb3">
       
<ComboBox.ItemsSource>
           
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
               
<Binding Path="Emps"/>
               
<Binding ElementName="cb1" Path="SelectedItem"/>
               
<Binding ElementName="cb2" Path="SelectedItem"/>
           
</MultiBinding>
       
</ComboBox.ItemsSource>
   
</ComboBox>
</StackPanel>
public class ComboBoxItemsSourceFilter : IMultiValueConverter
{
   
#region IMultiValueConverter Members

   
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   
{
       
var collection = new List<object>((object[])values[0]);
       
foreach (var item in values.Skip(1))
       
{
           
if (item != null) collection.Remove(item);
       
}
       
return collection;
   
}

   
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   
{
       
throw new NotSupportedException();
   
}

   
#endregion
}