Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind the 'enabled' property of a monotouch UI element to a viewmodel boolean in Mvvmcross

Writing my login page in IOS Droid using MVVMCross.

this is what I have so far

var bindingSet = this.CreateBindingSet<LoginPageView, LoginPageViewModel>();
bindingSet.Bind(this.UsernameTextField).To(x => x.UserName).TwoWay();
bindingSet.Bind(this.UsernameTextField).For(x=>x.Enabled).To(x => !x.LoggingIn);
bindingSet.Apply();

The binding of 'UserName' successfully binds to the UsernameTextField. However, when the LoginCommand is fired (excluded for brevity) I wan't UI control to be set as 'Enabled = false' whilst the login routine is in progress.

The code above fails at run-time at the x.Enabled binding, with

System.ArgumentException: Property expression must be of the form 'x => x.SomeProperty.SomeOtherProperty'

I must be writing the binding incorrectly, as I DO wish to bind to the 'Enabled' property directly, and not a child prop - but i can't quite see how to do this.

I have looked around some of the samples on mvvmcross, and have looked through a few N+1 videos but I can't seem to find a sample matching enabled, or another child property binding.

Thanks

like image 316
Patrick McCurley Avatar asked Sep 23 '13 10:09

Patrick McCurley


1 Answers

I couldn't see what was wrong with your code - but I just tried https://github.com/slodge/Enabling and that seemed to work...

So I took another look ... and the problem isn't in the Enabled - instead it is in:

      To(x => !x.LoggingIn)

That is not a simple property expression - it's got the ! operator there.

Instead of using ! you can use a ValueConverter like:

  public class InverseValueConverter : MvxValueConverter<bool, bool>
  {
      protected override bool Convert(bool value, ...)
      {
          return !value;
      }
  }

Then:

  bindingSet.Bind(this.UsernameTextField)
            .For(x=>x.Enabled)
            .To(x => x.LoggingIn)
            .WithConversion(new InverseValueConverter(), null);
like image 199
Stuart Avatar answered Oct 24 '22 18:10

Stuart