We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to disable Xamarin.Forms button


This article explains how to disable the button with command interface usage.

 

Typically, IsEnabled property is used to determine the interaction of the Xamarin.Forms button.

 

Consider If the use case is depending on command interface in button, using the IsEnabled property does not make any UI difference as per the MSDN guidelines stated “Do not use the IsEnabled property of Button, if you are using the command interface”.

 

This article will guide you how to change the enabled state of SfButton with Command interface, based on the toggle value of switch control as shown in the following code snippets.

 

[XAML]

..  
<Switch HorizontalOptions="Center"  VerticalOptions="Center" IsToggled="{Binding IsButtonEnabled}"  />
 
<buttons:SfButton 
            ..
            Text="Button" 
            FontSize="18" 
            Command="{Binding ButtonCommand}" >
</buttons:SfButton>
..

 

To handle their enabled or disabled state, you need to execute the CanExecute method (CanExecuteClickCommand) of buttons command. For getting this dynamic update, you also need to call ChangeCanExecute of buttons command in IsToggled binding property as shown in the following code snippet.

 

 [C#]

        …
 
        public Command ButtonCommand { get; set; }
 
        public bool IsButtonEnabled
        {
            get { return isButtonEnabled; }
            set
            {
                isButtonEnabled = value;
                ButtonCommand.ChangeCanExecute();
                OnPropertyChanged();
            }
        }
 
        bool CanExecuteClickCommand(object arg)
        {
            return isButtonEnabled;
        }
 
        public ViewModel()
        {
            ButtonCommand = new Command(ExecuteClickCommand, CanExecuteClickCommand);
        }
        
       
        void ExecuteClickCommand(object obj)
        {
            //Execute Xamarin.Forms SfButton Command action.
        }
 

Output

Dynamically handle the IsEnabled property of Xamarin.Forms Button

 

 

Download the complete sample here.

 

See Also:

What are the visual states available in Xamarin.Forms Button?

How to customize the Xamarin.Forms Button?

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Daniel Seesi
Jul 09, 2022

Very very helpful. Thanks a lot.

Reply
Chozarajan Pandiyarajan [Syncfusion]
Jul 11, 2022

Hi Daniel,

Thanks for the update.

Please let us know if you need any other details.

Thanks, Chozarajan P

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile