Can I enable or disable a command based on a condition in MAUI?

Platform: .NET MAUI| Category: Controls

Yes, you can conditionally enable or disable a command by implementing the CanExecute method. Here is an example:

bool CanSubmit = true; // Your condition or Boolean property
 Button myButton = new Button
 {
     Text = "Submit",
 };

 myButton.Command = new Command(() =>
 {
     DisplayAlert("Button Clicked", "Button was clicked!", "OK");
 }, () => CanSubmit);

 Content = new StackLayout
 {
     Children = { myButton },
     VerticalOptions = LayoutOptions.CenterAndExpand,
     HorizontalOptions = LayoutOptions.CenterAndExpand
 };

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.