How to convert behavior to generic type in Xamarin Forms?

Viewed 490

I have a Behavior which is responsible for coloring text if he is too short. I want to use behavior not only with Entry control but with a lot of others like Search Bar or Editor. How I can transform this Behavior to generic type? Is it possible? I hope you understand what I mean.

public class MinLengthValidatonBehavior : Behavior<Entry>
{
    public static readonly BindableProperty MinLengthProperty =
        BindableProperty.Create("MinLength", typeof(int), typeof(MinLengthValidatonBehavior), 0);

    public static readonly BindableProperty InvalidColorProperty =
        BindableProperty.Create("InvalidColor", typeof(string), typeof(EmailValidatonBehavior), "e4375b");

    public int MinLength
    {
        get => (int)GetValue(MinLengthProperty);
        set => SetValue(MinLengthProperty, value);
    }

    public string InvalidColor
    {
        get => GetValue(InvalidColorProperty).ToString();
        set => SetValue(InvalidColorProperty, value);
    }

    protected override void OnAttachedTo(Entry bindable)
    {
        bindable.TextChanged += BindableTextChanged;
    }

    private void BindableTextChanged(object sender, TextChangedEventArgs e)
    {
        (sender as Entry).TextColor =
            e.NewTextValue.Length < MinLength ? Color.FromHex(InvalidColor) : Color.Default;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.TextChanged -= BindableTextChanged;
    }
}
1 Answers
Related