I have an ObservableCollection bound to a ComboBox and during the program I add or remove elements to this collection. I want these to be reflected in the ComboBox but for some reason no change is being triggered. What am I doing wrong?
ComboBox:
<ComboBox x:Name="Cbox2" ItemsSource="{Binding ExportChoices}"
Height="30" Width="200" Margin="10" FontSize="18"
HorizontalAlignment="Center" Grid.Column="1"/>
Code behind with method in which I remove items from bound Observable Collection:
public partial class ExportPage : Page
{
private ObservableCollection<String> exportChoices = new();
public ObservableCollection<String> ExportChoices
{
get { return exportChoices; }
set
{
exportChoices = value;
}
}
public ExportPage()
{
InitializeComponent();
this.DataContext = this;
ExportChoices = new()
{
"Name",
"Northing",
"Easting",
"Elevation",
"Description"
};
}
private void Cbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
switch (comboBox.Name)
{
case "Cbox1":
if (!ExportChoices.Contains(Cbox1Selection))
{
ExportChoices.Add(Cbox1Selection);
}
Cbox1Selection = comboBox.SelectedItem.ToString();
break;
case "Cbox2":
if (!ExportChoices.Contains(Cbox2Selection))
{
ExportChoices.Add(Cbox2Selection);
}
Cbox2Selection = comboBox.SelectedItem.ToString();
break;
case "Cbox3":
if (!ExportChoices.Contains(Cbox3Selection))
{
ExportChoices.Add(Cbox3Selection);
}
Cbox3Selection = comboBox.SelectedItem.ToString();
break;
case "Cbox4":
if (!ExportChoices.Contains(Cbox4Selection))
{
ExportChoices.Add(Cbox4Selection);
}
Cbox4Selection = comboBox.SelectedItem.ToString();
break;
case "Cbox5":
if (!ExportChoices.Contains(Cbox5Selection))
{
ExportChoices.Add(Cbox5Selection);
}
Cbox5Selection = comboBox.SelectedItem.ToString();
break;
}
if (ExportChoices.Contains(comboBox.SelectedItem.ToString()))
{
ExportChoices.Remove(comboBox.SelectedItem.ToString());
}
}
}