This example demonstrates how to to find a way to disable / enable the SfButton when using ICommand. As per the MSDNsuggestion.
Do not use the IsEnabled property of Button, if you are using the command interface.
Please refer KB links for more details,
How to disable the Xamarin Button[SfButton]
Let us have a demo sample with Switch control, and we have changed the enable option of Button based on its IsToggled of Switch.
[XAML]
..
<Switch HorizontalOptions="Center" VerticalOptions="Center" IsToggled="{Binding IsButtonEnabled}" />
<buttons:SfButton
..
Text="Button"
FontSize="18"
Command="{Binding ButtonCommand}" >
</buttons:SfButton>
..
Invoke the CanExecute method of button's command while changing the model property of IsButtonEnabled (IsToggled's model property) and, based on the return value of CanExecuteClickCommand, we can enable or disable the Button.
[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.
}
See Also :
What are the visual states available in Xamarin.Forms Button?
How to customize the Xamarin.Forms Button?
Also refer our feature tour page to know more features available in our button.
If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.