MVVMCross MvxBind, binding multiple values to one property
-
21-12-2019 - |
Question
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"/>
Solution
Using || instead of | actually resolved this issue.
local:MvxBind="Enabled (IHaveDoneThis || IHaveDoneThat)"
OTHER TIPS
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); }
}