Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin.Android Cannot disable a button by binding "Enabled" which has binding "Click" #4046

Closed
hungnguyenepiserver opened this issue Jan 11, 2021 · 3 comments
Labels
s/invalid Invalid status

Comments

@hungnguyenepiserver
Copy link

🐛 Bug Report

Xamarin.Android Cannot disable a button by binding "Enabled" which has binding "Click"

Expected behavior

Button can be disabled by binding "Enabled" in .axml code, not depends or relate to "Click" event of "Click" Binding

Reproduction steps

  1. Binding Click command and Enabled state variable to a button
  2. Click command has logic to change "Enabled" state
  3. Run app
  4. Click to the button

Configuration

Version: 7.x

Platform:

  • Xamarin.Android

Some code:

private async Task ClickEvent()
{
this.EnableBtn = !this.EnableBtn;
}

    private bool enableBtn;
    public bool EnableBtn
    {
        get => enableBtn;
        set
        {
            enableBtn = value;
            RaisePropertyChanged(() => EnableBtn);
        }
    }
    
    private MvxAsyncCommand clickCommand;
    public MvxAsyncCommand ClickCommand
    {
        get
        {
            this.clickCommand = this.clickCommand ?? new MvxAsyncCommand(this.ClickEvent);
            return this.clickCommand;
        }
    }

@elstringo
Copy link

elstringo commented Feb 19, 2021

Are you aware you can get the desired behaviour by using a 'CanExecute' Func/method for your MvxAsyncCommand, and not binding to the Enabled property?
So something like this...


private MvxAsyncCommand clickCommand;
public MvxAsyncCommand ClickCommand
{
    get
    {
        clickCommand = clickCommand ?? new MvxAsyncCommand(ClickCommandExecute, ClickCommandCanExecute);
        return clickCommand;
    }
}

private async Task ClickCommandExecute()
{
    EnableButton = !EnableButton;
    await Task.CompletedTask;
}

private bool ClickCommandCanExecute()
{
    return EnableButton;
}

private bool enableBtn;
public bool EnableBtn
{
    get => enableBtn;
    set
    {
        if (SetProperty(ref enableBtn, value))
        {
            ClickCommand.RaiseCanExecuteChanged();
        }
    }
}

@Cheesebaron Cheesebaron added the s/invalid Invalid status label Feb 19, 2021
@hungnguyenepiserver
Copy link
Author

Are you aware you can get the desired behaviour by using a 'CanExecute' Func/method for your MvxAsyncCommand, and not binding to the Enabled property?
So something like this...


private MvxAsyncCommand clickCommand;
public MvxAsyncCommand ClickCommand
{
    get
    {
        clickCommand = clickCommand ?? new MvxAsyncCommand(ClickCommandExecute, ClickCommandCanExecute);
        return clickCommand;
    }
}

private async Task ClickCommandExecute()
{
    EnableButton = !EnableButton;
    await Task.CompletedTask;
}

private bool ClickCommandCanExecute()
{
    return EnableButton;
}

private bool enableBtn;
public bool EnableBtn
{
    get => enableBtn;
    set
    {
        if (SetProperty(ref enableBtn, value))
        {
            ClickCommand.RaiseCanExecuteChanged();
        }
    }
}

Thanks for your idea. @elstringo
But I'm considering using Enable because of I also want the button change the color following enable change.

@o0k4m1
Copy link

o0k4m1 commented Sep 26, 2022

I have resolved this by switching places of bind items:

        app:MvxBind="Click AddNewItemCommand;Enabled IsValid"

So this might be a temporary solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s/invalid Invalid status
Development

No branches or pull requests

4 participants