Ryan Langton
Ryan Langton

Reputation: 6160

MvvmCross Validation Binding visibility

I followed the instructions here to bind my view model validation to the input form. Using MVVMCross to bind to error messages

The problem I have now is that there is a lot of extra spacing on the form due to the validation elements. How do I make these spacing problems go away? It's a bit difficult to use a Visibility converter due to the fact there is no property for each field. Same problem with Android and iOS. I suppose maybe some sort of custom visibility converter?

Upvotes: 1

Views: 3014

Answers (1)

Stuart
Stuart

Reputation: 66882

I think a quick fix might be to use a binding like Visible Errors['Email'] - however, you're reporting that's not working (so transferred that to https://github.com/MvvmCross/MvvmCross/issues/494 - thanks)

Since that doesn't work directly, then you you should be able to bind the Visible boolean property using something like (in Android):

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textColor="#ff0000"
  android:text="My error text"
  local:MvxBind="Visible ErrorExists(Errors['Email'],FallbackValue=null)"
    />

where ErrorExists is:

public class ErrorExistsValueConverter : MvxValueConverter
{
    public override object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value != null);
    }
}

For iOS, if you are showing/hiding UIViews, then you would need to ensure your UI layout auto-updates - e.g. using constraints


As an alternative UI technique, you should also be able to use binding on the background color of an EditText - similar to the color binding in https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Colors.axml

Upvotes: 4

Related Questions