Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Custom Validator and Value from Binding

$
0
0

I have an entry for the number of items i want to buy.
This number can contain a variable number of decimals, decided by a DecimalCode field, that can be 1,2 or 3 decimals.
I want my Validator, to be able to correctly validate the number based on the DecimalCode

My Xaml:

<Entry Text="{Binding Antal}" Style="{StaticResource VareEntry}">
    <Entry.Behaviors>
        <behaviors:OrdreLinjeAntalValidator DecimalKode="{Binding DecimalKode}" />
    </Entry.Behaviors>
</Entry>

My Class im binding to:

public class OrdreLinje
{
    public double Antal { get; set; }
    public int DecimalKode { get; }
}

The error:
Xamarin.Forms.Xaml.XamlParseException: Position 79:57. Cannot assign property "DecimalKode": Property does not exists, or is not assignable, or mismatching type between value and property

The weird thing, is this works when i hardcode the value, like this

<Entry Text="{Binding Antal}" Style="{StaticResource VareEntry}">
    <Entry.Behaviors>
        <behaviors:OrdreLinjeAntalValidator DecimalKode="2" />
    </Entry.Behaviors>
</Entry>

The Validator Class is here

    public class OrdreLinjeAntalValidator : Behavior<Entry>
    {
        static readonly BindablePropertyKey IsValidPropertyKey =
            BindableProperty.CreateReadOnly(nameof(IsValid), typeof(bool), typeof(OrdreLinjeAntalValidator), defaultValue: false);

        public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

        static readonly BindableProperty DecimalKodeProperty =
            BindableProperty.Create(nameof(DecimalKode), typeof(int), typeof(OrdreLinjeAntalValidator), defaultValue: 0);
        public bool IsValid
        {
            get { return (bool)GetValue(IsValidProperty); }
            private set { SetValue(IsValidPropertyKey, value); }
        }

        public int DecimalKode
        {
            get { return (int)GetValue(DecimalKodeProperty); }
            set { SetValue(DecimalKodeProperty, value); }
        }

        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += HandleTextChanged;
        }
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= HandleTextChanged;

        }

        // Example Reg ^\d*\.\d{3}$
        // We use String concat to make it readable
        const string regex1 = @"^\d*(\.\d{0,";
        const string regex2 = @"})?$";
        void HandleTextChanged(object sender, TextChangedEventArgs e)
        {
            if (e.NewTextValue != null) // This happens when users click Cancel
            {
                IsValid = (Regex.IsMatch(e.NewTextValue, regex1 + DecimalKode + regex2, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
                ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
            }
        }
    }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>