How to set combox item visibility?
15,898
Solution 1
You can use the Method that sa_ddam213 mentioned, or you can just brute force it in the SelectionChanged Event like so.
private void comboboxA_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
for (int i = 0; i <= comboboxB.Items.Count -1; i++)
{
if (((ComboBoxItem)(comboboxB.Items[i])).Content.ToString() == ((ComboBoxItem)comboboxA.SelectedItem).Content.ToString())
{
((ComboBoxItem)(comboboxB.Items[i])).Visibility = System.Windows.Visibility.Collapsed;
}
else
((ComboBoxItem)(comboboxB.Items[i])).Visibility = System.Windows.Visibility.Visible;
}
}
Solution 2
Maybe just filtering the selected item from list
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Grid>
<ComboBox ItemsSource="{Binding ElementName=UI,Path=YourCollection}" SelectedItem="{Binding ElementName=UI,Path=SelectedItem}" Height="23" HorizontalAlignment="Left" Margin="65,61,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding ElementName=UI, Path=FilteredCollection}" Height="23" HorizontalAlignment="Left" Margin="223,61,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _selectedItem;
private ObservableCollection<string> _yourCollection = new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
YourCollection.Add("Apple");
YourCollection.Add("Banana");
YourCollection.Add("Pear");
YourCollection.Add("Orange");
NotifyPropertyChanged("FilteredCollection");
}
// Collection Fro ComboBox A
public ObservableCollection<string> YourCollection
{
get { return _yourCollection; }
set { _yourCollection = value; }
}
// ComboBox A selected Item
public string SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
// Notify the the filter collection has changed
NotifyPropertyChanged("FilteredCollection");
}
}
// Collection to show in ComboBox B
public List<string> FilteredCollection
{
// Remove the selected Item
get { return _yourCollection.Where(s => !s.Equals(_selectedItem)).ToList(); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Or do you need this to work both ways
Author by
Nerdynosaur
Updated on August 21, 2022Comments
-
Nerdynosaur over 1 year
i have 2 WPF comboboxes(comboboxA, comboboxB)with identical combobox item(Apple & Orange). Let say i select "Apple" in the comboboxA, then the "Apple" need to be hidden in the comboxB. If i go back to comboxA and select "Orange", "Apple" will be visible and "Orange" need to be hidden. How can i achieve that using C#?
code snippet for xaml:
<ComboBox Name="comboboxA" > <ComboBoxItem Content="Apple" Name="AppleA"></ComboBoxItem> <ComboBoxItem Content="Orange" Name="OrangeA"></ComboBoxItem> </ComboBox> <ComboBox Name="comboboxB" > <ComboBoxItem Content="Apple" Name="AppleB"></ComboBoxItem> <ComboBoxItem Content="Orange" Name="OrangeB"></ComboBoxItem> </ComboBox>
-
Nerdynosaur over 11 yearsDo i need to add any reference in order to use the "ObservableCollection" ?
-
sa_ddam213 over 11 yearsYes you need to add:
using System.Collections.ObjectModel;
Or you could just make it a List<string>, but you will have to call NotifyPropertyChanged("YourCollection") after adding items. The Observable collection has this function built in and will update the UI when items are added/removed so this is a very handy list type for WPF -
Nerdynosaur over 11 yearshmmm.....nothing show up in the combobox. Just blank when i click on both of the combobox
-
sa_ddam213 over 11 yearsDid you add
Name="UI"
to the XAML in the Window part, Feel free to post up you xmal,code so I can help :) -
Nerdynosaur over 11 yearsThanks mark, yours work very well too C=. Btw i got another question. Since i select "apple" in comboboxA, the "apple" will be hidden in comboxB. But there is still a white space for the "apple". Can i simply remove the white space too?
-
Mark Hall over 11 years@0070 try using `System.Windows.Visibility.Collapsed instead of Hidden see edit