Reputation: 4248
Suppose we have four ComboBoxes in view (source):
<StackPanel>
<ComboBox ItemsSource="{Binding SourceCollection1}" DisplayMemberPath="Name" SelectedItem="{Binding Selected1}"/>
<ComboBox ItemsSource="{Binding SourceCollection2}" DisplayMemberPath="Name" SelectedItem="{Binding Selected2}"/>
<ComboBox ItemsSource="{Binding SourceCollection3}" DisplayMemberPath="Name" SelectedItem="{Binding Selected3}"/>
<ComboBox ItemsSource="{Binding SourceCollection4}" DisplayMemberPath="Name" SelectedItem="{Binding Selected4}"/>
</StackPanel>
And the VM something like (source):
public ObservableCollection<People> SourceCollection1 { get; set; }
...
private People _selected1;
public People Selected1
{
get
{
return _selected1;
}
set
{
var pc = PropertyChanged;
if (pc != null)
{
_selected1 = value;
pc(this, new PropertyChangedEventArgs("Selected1"));
}
}
}
...
And the People
class has two properties: Name
and Age
.
So, I'd like to implement this feature: when user selected one item in ComboBox1
, then the remaining ConboBoxes should adapt their own ItemsSource, removing the items which have been selected in other ComboBoxes.
For example, the application start up, and no item has been selected yet. Then user select people A
in ComboBox1
, so when user opens ComboBox2
dropdown list, the people A
item shouldn't be include in the ComboBox2
source. So when user select people B
in ComboBox2
, the ComboBox3
shouldn't have people A
and people B
. And so on...
Does anyone have some nice solutions?
Upvotes: 0
Views: 1142
Reputation: 2315
This could work:
public People Selected1
{
get
{
return _selected1;
}
set
{
var pc = PropertyChanged;
UpdateList (SourceCollection2, _selected, value);
if (pc != null)
{
_selected1 = value;
pc(this, new PropertyChangedEventArgs("Selected1"));
}
}
}
private void UpdateList(ObservableCollection<People> list, People oldItem, People newItem){
if(!list.Contains(oldItem)){
list.Add(oldItem);
}
if(list.Contains(newItem)){
list.Remove(newItem);
}
}
Upvotes: 1