3

How do I use a combination of booleans values, to set the enabled property on a MvxBind button?

For example:

Using one boolean value, the binding is achieved with:

<Button
      android:text="Next"
      local:MvxBind="Enabled IHaveDoneEverything"/>

But how do I implement this using multiple boolean values?

Things I've tried that didn't work:

  • Using an OR statement in axml. local:MvxBind="Enabled (IHaveDoneThis | IHaveDoneThat)"
  • Using an extra property in my ViewModel. This didn't work due to the property not being 'set' and thus not being updated in the view.

    public bool IHaveDoneAtleastSomething 
    { 
        get { return (IHaveDoneThis | IHaveDoneThat); } 
    }
    
  • Using a custom valueconverter.

    local:MvxBind="Enabled [IHaveDoneThis , IHaveDoneThat], Converter=MultipleBooleansToOneBooleanUsingORValueConverter"/>
owe
  • 4,890
  • 7
  • 36
  • 47
MichielDeRouter
  • 426
  • 4
  • 21
  • 1
    Have you tried Logical rather than Bitwise OR? See answer in http://stackoverflow.com/questions/3154132/what-is-the-difference-between-logical-and-conditional-and-or-in-c and parser in https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/Parse/Binding/Tibet/MvxTibetBindingParser.cs#L120 - in addition, I think you should be able to use `Or(Expr1, Expr2)` – Stuart Feb 26 '14 at 09:54

2 Answers2

5

Using || instead of | actually resolved this issue.

local:MvxBind="Enabled (IHaveDoneThis || IHaveDoneThat)"
MichielDeRouter
  • 426
  • 4
  • 21
1

For future reference, in order to get the extra ViewModel property working you would have to make sure that the ViewModel calls the RaisePropertyChanged method for the extra property whenever IHaveDoneThis or IHaveDoneThat changed otherwise the binding won't trigger.

What I usually do is something like:

private bool _internalIHaveDoneThis;
public bool IHaveDoneThis
{
    get{return _internalIHaveDoneThis;} 
    set
    {
        if(_internalIHaveDoneThis != value)
        {
            _internalIHaveDoneThis = value; 
            RaisePropertyChanged(() => IHaveDoneThis);
            RaisePropertyChanged(() => IHaveDoneAtleastSomething);
        }
    }
}


private bool _internalIHaveDoneThat;
public bool IHaveDoneThat
{
    get{return _internalIHaveDoneThat;} 
    set
    {
        if(_internalIHaveDoneThat != value)
        {
            _internalIHaveDoneThat = value; 
            RaisePropertyChanged(() => IHaveDoneThat);
            RaisePropertyChanged(() => IHaveDoneAtleastSomething);
        }
    }
}

public bool IHaveDoneAtleastSomething 
{ 
    get { return (IHaveDoneThis | IHaveDoneThat); } 
}
isaacmcn
  • 98
  • 4
  • 1
    Also for future reference, there are some projects which try to help you do this automagically - especially http://slodge.blogspot.co.uk/2013/07/intercepting-raisepropertychanged.html and http://slodge.blogspot.co.uk/2013/06/intercepting-raisepropertychanged.html and also Fody - http://slodge.blogspot.co.uk/2013/07/awesome-clean-viewmodels-via-fody.html - but for one-off cases all of these are overkill :) – Stuart Feb 26 '14 at 21:35
  • Thanks for that Stuart- Fody looks awesome. Have been generating the properties with RaisePropertyChanged stuff via T4 templates, but this looks even better! – isaacmcn Feb 26 '14 at 22:38