diff --git a/BookAStar/BookAStar/Behaviors/MaxLengthValidator.cs b/BookAStar/BookAStar/Behaviors/MaxLengthValidator.cs index e35a5d14..07f51476 100644 --- a/BookAStar/BookAStar/Behaviors/MaxLengthValidator.cs +++ b/BookAStar/BookAStar/Behaviors/MaxLengthValidator.cs @@ -7,6 +7,35 @@ using Xamarin.Forms; namespace BookAStar.Behaviors { + public class EditorMaxLengthValidator : Behavior + { + public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create("MaxLength", typeof(int), typeof(MaxLengthValidator), 0); + + public int MaxLength + { + get { return (int)GetValue(MaxLengthProperty); } + set { SetValue(MaxLengthProperty, value); } + } + + protected override void OnAttachedTo(Editor bindable) + { + bindable.TextChanged += bindable_TextChanged; + } + + public bool IsValid { get; set; } + + private void bindable_TextChanged(object sender, TextChangedEventArgs e) + { + IsValid = e.NewTextValue.Length <= 0 || e.NewTextValue.Length <= MaxLength; + if (!IsValid && e.NewTextValue.Length > MaxLength) + ((Editor)sender).Text = e.NewTextValue.Substring(0, MaxLength); + } + + protected override void OnDetachingFrom(Editor bindable) + { + bindable.TextChanged -= bindable_TextChanged; + } + } public class MaxLengthValidator : Behavior { public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create("MaxLength", typeof(int), typeof(MaxLengthValidator), 0); diff --git a/BookAStar/BookAStar/Pages/EditBillingLinePage.xaml b/BookAStar/BookAStar/Pages/EditBillingLinePage.xaml index 297171e6..0fec3c0b 100644 --- a/BookAStar/BookAStar/Pages/EditBillingLinePage.xaml +++ b/BookAStar/BookAStar/Pages/EditBillingLinePage.xaml @@ -43,8 +43,14 @@ - - + + + + + + @@ -78,4 +84,4 @@ Command="{Binding ValidateCommand}" Clicked="OnValidateClicked"> - \ No newline at end of file +