Xamarin – Validate string with a value converter

So I wrote this quite a while ago to check a string input and just decided to apply it to validation like so…

   public class ValidStringContentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var s = value as string;
            try
            {
                if (!string.IsNullOrEmpty(s))
                {
                    return "YOUR GOOD / PASS IMAGE HERE";
                }
                else
                {
                    return "YOUR BAD / FAIL IMAGE HERE";
                }

            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached) Debug.WriteLine(ex.Message);
                return "YOUR BAD / FAIL IMAGE HERE";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

After that it’s just a standard value converter in XAML…

 

Here is another way using Fody

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s