ComboBox Selection Change keeps looping

169 views Asked by At

I am currently developing a WPF app in which there are multiple forms to manage a database. One of these forms is a "Add form". This form contains a combobox with some values. Though if the user's wanted value is not in the combobox list, it can be added by selecting the last option which will display a new form for entering the wanted value. If the form is submitted, the last option in the combobox will be changed to this value. The code below describes how this works:

        private void BrandBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (BrandBox.SelectedItem != null &&
                BrandBox.SelectedIndex < BrandBox.Items.Count - 1)
            {
                InitSubCategoryBox(subCatArray[BrandBox.SelectedIndex]);
            }
            else if (BrandBox.SelectedItem != null &&
                     BrandBox.SelectedIndex >= BrandBox.Items.Count - 1) 
            {
                OtherInputWindow newInputForm = new OtherInputWindow("brand", BrandBox.SelectedItem.ToString());

                if (newInputForm.ShowDialog() == true)
                {
                    BrandBox.Items[BrandBox.Items.Count - 1] = newInputForm.returnValue;
                    BrandBox.SelectedIndex = BrandBox.Items.Count - 1;
                }
            } 
        }

The last code line will set the submitted value as selected item. I know this does not work. This last line of code will create a SelectionChanged event and that is exactly what this method is. The result is that it will keep looping and there is no way out. I'm searching for the right way to do this for a long time but I wasn't able yet to find the answer. Hopefully I will find the answer here. Is there someone who can help me solving this problem?

1

There are 1 answers

12
BionicCode On

I would suggest to not change the SelectedItem from the SelecetionChanged event handler. Instead handle the UIElement.PreviewMouseLeftButtonUp event from the ComboBox:

<ComboBox PreviewMouseLeftButtonUp="OnComboBoxItemPreviewMouseLeftButtonUp" />
private void OnComboBoxItemPreviewMouseLeftButtonUp(object sender, RoutedEventArgs e)
{
  var selector = sender as Selector;
  var clickedItemContainer = Selector.ContainerFromElement(selector, e.OriginalSource as DependencyObject);
  bool isClickTargetItemContainer = clickedItemContainer is not null;
  if (!isClickTargetItemContainer)
  {
    return;
  }

  int selectedIndex = selector.ItemContainerGenerator.IndexFromContainer(clickedItemContainer);
  int lastIndex = selector.Items.Count - 1;
  if (selector.SelectedIndex == lastIndex)
  {
    var newInputForm = new OtherInputWindow("brand", selectedItem.ToString());

    if (newInputForm.ShowDialog() == true)
    {
      selector.Items[lastIndex] = newInputForm.returnValue;
      selector.SelectedIndex = lastIndex;
    }
  }
  else
  {
    InitSubCategoryBox(subCatArray[selector.SelectedIndex]);
  }
}