0
votes

I am new to MVVMCross framework for Android and having trouble on how to enable/disable a button. I wasn't able to find a documentation around this area.

Code ViewModel:

    private bool _buttonEnabled;
    public bool ButtonEnabled
    {
        get
        { return string.IsNullOrEmpty(EmailLogin);}
        set
        {
            _buttonEnabled = value;
            RaisePropertyChanged(() => ButtonEnabled);
        }
    }

Android axml:

 <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Login"
    android:background="@color/yellow"
    local:MvxBind="Enabled(ButtonEnabled)"/>

But unable to get it to work. What I'm trying to achieve is that if EmailLogin variable is NullOrEmpty then button should be disabled. Where am i going wrong ?

Hopefully i'm not pushing this but can i add additional binding to it so that if it is disabled or enabled i can change the color of the button ?

1

1 Answers

2
votes

You just need to change the binding syntax to this:

 <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Login"
    android:background="@color/yellow"
    local:MvxBind="Enabled ButtonEnabled"/>

I don't know if you are doing it because your code doesn't show everything, but you will also need to call RaisePropertyChanged(() => ButtonEnabled); inside the setter of your EmailLogin property.

Also if you want to change the color of your button, you can install the official color plugin. Usage would be something like this:

private MvxColor _myColor;
public MvxColor MyColor
{
    get
    {   return _myColor; }
    set
    {
        _myColor = value;
        RaisePropertyChanged(() => MyColor);
    }
}

And then: local:MvxBind="Enabled ButtonEnabled; TextColor MyColor"