.NET MAUI ComboBox Commands
The .NET MAUI ComboBox provides the following commands:
-
SelectAllCommand
(ICommand
)—Selects all items from the source.
The
SelectAll
command can be used only whenSelectionMode
isMultiple
. An exception will be thrown if the command is invoked inSingle
SelectionMode
.
-
ClearSelectionCommand
(ICommand
)—Sets the selection to null. If MultipleSelectionMode
is used, this command will clear all selected items. YOu can override the default behavior and create custom command.
The example below shows both cases, the default ClearSelectionCommand
execution and custom ClearSelectionCommand
implementation:
<HorizontalStackLayout Spacing="5"
Margin="0, 0, 0, 12">
<Button Text="Default Clear Command"
Command="{Binding Source={x:Reference comboBox}, Path=ClearSelectionCommand}"
AutomationId="customClearButton"/>
<Button Text="Custom Clear Command"
Clicked="ExecuteCustomCommanClicked"
AutomationId="defaultClearButton"/>
</HorizontalStackLayout>
<telerik:RadComboBox x:Name="comboBox"
Placeholder="Select City!"
SelectionMode="Multiple"
IsDropDownClosedOnSelection="False"
WidthRequest="{OnPlatform MacCatalyst=300, WinUI=300}"
HorizontalOptions="{OnIdiom Default=Fill, Desktop=Start}"
ItemsSource="{Binding Items}"
DisplayMemberPath="Name"
AutomationId="comboBox"/>
class CustomClearSelectionCommand : ComboBoxClearSelectionCommand
{
public override async void Execute(object parameter)
{
bool executeDefault = await App.Current.MainPage.DisplayAlert("Command", "Execute command?", "Yes", "No");
if (executeDefault)
{
base.Execute(parameter);
}
}
}
private void ExecuteCustomCommanClicked(object sender, EventArgs e)
{
this.comboBox.ClearSelectionCommand = new CustomClearSelectionCommand();
this.comboBox.ClearSelectionCommand.Execute(true);
}