Articles in this section
Category / Section

How to disable Xamarin.Forms button

1 min read

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?

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied