using System; using Xamarin.Forms; namespace Bit.App.Behaviors { public class RequiredValidationBehavior : Behavior { private static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(RequiredValidationBehavior), false); public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty; public bool IsValid { get { return (bool)GetValue(IsValidProperty); } private set { SetValue(IsValidPropertyKey, value); } } protected override void OnAttachedTo(Entry entry) { entry.TextChanged += HandleTextChanged; base.OnAttachedTo(entry); } void HandleTextChanged(object sender, TextChangedEventArgs e) { IsValid = !string.IsNullOrWhiteSpace(e.NewTextValue); ((Entry)sender).BackgroundColor = IsValid ? Color.Default : Color.Red; } protected override void OnDetachingFrom(Entry entry) { entry.TextChanged -= HandleTextChanged; base.OnDetachingFrom(entry); } } }