Bitwarden-app-android-iphon.../src/App/Behaviors/RequiredValidationBehavior.cs

34 lines
1.1 KiB
C#

using System;
using Xamarin.Forms;
namespace Bit.App.Behaviors
{
public class RequiredValidationBehavior : Behavior<Entry>
{
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 bindable)
{
bindable.TextChanged += HandleTextChanged;
}
void HandleTextChanged(object sender, TextChangedEventArgs e)
{
IsValid = !string.IsNullOrWhiteSpace(e.NewTextValue);
((Entry)sender).BackgroundColor = IsValid ? Color.Default : Color.Red;
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= HandleTextChanged;
}
}
}