You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
privateint_index=0;// Default to first item workspublicintIndex{get => _index;private set
{if(value==-1){_index=0;// Issue is here because -1 was not successfully set
OnPropertyChanged(nameof(Index));}elseif(value!=_index){_index=value;
OnPropertyChanged(nameof(Index));}}}privateIEnumerable<string>_selectableItems=new[]{"Default"};publicIEnumerable<string>SelectableItems{get => _selectableItems;privateset{_selectableItems= value;
OnPropertyChanged(nameof(SelectableItems));}}privatevoidUpdateItems(object[]items){// Setting selectable items sets the selected index to -1 for no reasonif(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:
privatebyte_trueValue;// Cannot be -1 (and I do not want -1 selection on a ComboBox anyway)privatebool_avaloniaIssue=true;publicintIndex{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.
The text was updated successfully, but these errors were encountered:
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.
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.
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:
The workaround returns -1 when this issue would occur, and then I immediately set the index back to 0.
The text was updated successfully, but these errors were encountered: