ComboBox (Dropdown)
Introduction
The ComboBox control provides a compact way for users to select from a list of options.
Code Example: Adding a ComboBox
// Initialize
var comboBox = new ComboBox();
comboBox.AddToRoot();
for(int i = 0; i < 10; i++)
{
comboBox.Items.Add($"Item {i}");
}
Closing the Drop-Down
Pressing Escape closes an open drop-down and keeps the current selection. This works however the drop-down was opened. Escape pressed while the drop-down is closed is left for other controls to use.
DropDownOpened and DropDownClosed
DropDownOpened is raised once each time the drop-down opens. DropDownClosed is raised once each time an open drop-down closes. Both are raised however the change happened, so a handler does not need to cover each route separately:
DropDownOpened: mouse, keyboard, gamepad, or settingIsDropDownOpentotrueDropDownClosed: picking an item, Escape, clicking away, losing focus, theComboBoxbeing disabled, hidden, or removed, or settingIsDropDownOpentofalse
Adjusting the Drop-Down ListBox
ComboBox provides a ListBox property which can be used to customize the ListBox instance.
This can be modified, even if it hasn't yet been shown. The following code shows how to modify the default size of the ListBox:

The ListBox property returns a fully functional ListBox instance. For more information on how to customize the ListBox, see the ListBox page.
Performance with Many Items
If all items in the ComboBox dropdown are the same height (no text wrapping or variable-sized icons), you can improve dropdown open performance by setting UseFixedStackChildrenSize on the ListBox's inner panel. This tells the layout engine to measure only the first child and assume all others are the same size, skipping per-child measurement.
SelectedObject and SelectedIndex
Use SelectedIndex to select an item by its zero-based position in Items, or read it back to find which item is currently selected. Use SelectedObject to get or set the selected item directly as an object.
SelectionChanged
The SelectionChanged event fires whenever the user picks a different item. The handler receives a SelectionChangedEventArgs with information about the previously and newly selected items. You can also read SelectedObject or SelectedIndex directly inside the handler.
Last updated
Was this helpful?

