Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComboBox selection is hidden if SelectedIndex does not return "-1" between #4472

Closed
Kermalis opened this issue Aug 10, 2020 · 1 comment
Closed
Labels

Comments

@Kermalis
Copy link
Contributor

This example has a model that has a ComboBox of string objects. If I update the items of the ComboBox, then the selection is hidden no matter what order I fire events and no matter if the old index is available in range of the new items. I have to manually click the empty ComboBox and select my item in the UI because it's not working in code-behind at all.

<ComboBox Items="{Binding SelectableItems}" SelectedIndex="{Binding Index}" />
private int _index = 0; // Default to first item works
public int Index
{
  get => _index;
  private set
  {
    if (value == -1)
    {
      _index = 0; // Issue is here because -1 was not successfully set
      OnPropertyChanged(nameof(Index));
    }
    else if (value != _index)
    {
      _index = value;
      OnPropertyChanged(nameof(Index));
    }
  }
}
private IEnumerable<string> _selectableItems = new[] { "Default" };
public IEnumerable<string> SelectableItems
{
  get => _selectableItems;
  private set
  {
    _selectableItems = value;
    OnPropertyChanged(nameof(SelectableItems));
  }
}

private void UpdateItems(object[] items)
{
  // Setting selectable items sets the selected index to -1 for no reason
  if (items.Length == 0)
  {
    SelectableItems = new[] { "Default" };
  }
  else
  {
    SelectableItems = items.Select(o => o.ToString());
  }
  Index = 0; // No selection even though I'm telling it to select 0 here and in the setter
  /* Same nonsense if you fire in reverse order
  Index = 0;
  SelectableItems = items;
  */
}

The issue goes away if you let -1 be successfully set (and then change the selected index again right after). This is not going to work when you are using unsigned types for your selected index, so you would need to work around it like this:

private byte _trueValue; // Cannot be -1 (and I do not want -1 selection on a ComboBox anyway)
private bool _avaloniaIssue = true;
public int Index
{
    get => _avaloniaIssue ? -1 : (int)_trueValue;
    set
    {
        if (value == -1)
        {
            _avaloniaIssue = true;
            OnPropertyChanged(nameof(Index));
        }
        else
        {
            _avaloniaIssue = false;
            _trueValue = (byte)value;
            OnPropertyChanged(nameof(Index));
        }
    }
}

The workaround returns -1 when this issue would occur, and then I immediately set the index back to 0.

@grokys
Copy link
Member

grokys commented Sep 15, 2020

Hmm, this in fact seems to be the same issue as #4048 - the problem is that the selected item isn't preserved when Items is changed. Closing this in favor of #4048 - should have a fix soon.

@grokys grokys closed this as completed Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants