Create reusable RequiredField custom control with behavior and style-converter?

DrEmigiusDrEmigius DEUniversity ✭✭

Hi all,
i have a question regarding custom controls.
I want to make a requiredentry-control and did achieve that by adding the following xaml:
<Entry Text="{Binding MyBinding}" Style="{Binding Source={x:Reference ReqFieldBehave}, Path=HasValue, Converter={StaticResource BoolToAlertStyleConverter}}"> <Entry.Behaviors> <behavior:RequiredFieldBehavior x:Name="ReqFieldBehave"/> </Entry.Behaviors> </Entry>
Where the behavior is validating the textinput and sets its own "HasValue" bindableproperty.
The Converter takes the boolean from "HasValue" and chooses a style from my globalresource Collection to show the entry in an "alerted" style when the entered text is empty.

Code:

public class RequiredFieldBehavior : Behavior<Entry>
    {
        static readonly BindablePropertyKey HasValuePropertyKey = 
        BindableProperty.CreateReadOnly("HasValue", typeof(bool), typeof(RequiredFieldBehavior), false);
        public static readonly BindableProperty HasValueProperty = HasValuePropertyKey.BindableProperty;

        public bool HasValue
        {
            get { return (bool)base.GetValue(HasValueProperty); }
            private set { base.SetValue(HasValuePropertyKey, value); }
        }
        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += HandleTextChanged;
            base.OnAttachedTo(bindable);
        }
        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= HandleTextChanged;
            base.OnDetachingFrom(bindable);
        }
        void HandleTextChanged(object sender, TextChangedEventArgs e)
        {
            HasValue = !string.IsNullOrEmpty(e.NewTextValue);
        }
    }

NOW i dont want to define the above xaml for every requiredfield i have on my form, so i was planning on creating a custom entrycontrol "RequiredEntry" but its not working.
First of all i dont know if and how i can set the style as shown above in codebehind. The Problem being the named reference to the behavior...
Second try via a custom xaml Control did not work... When i created the custom Control which inherited from Entry i was not able to set properies in the inherited Control, it was just ignored.

I had that:
<Entry xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:behavior="clr-namespace:MyProject.View.Behavior;assembly=MyProject" x:Class="MyProject.View.Control.RequiredEntry"> <Entry TextColor="Fuchsia" Style="{Binding Source={x:Reference ReqFieldBehave}, Path=HasValue, Converter={StaticResource BoolToAlertStyleConverter}}"> <Entry.Behaviors> <behavior:RequiredFieldBehavior x:Name="ReqFieldBehave"/> </Entry.Behaviors> </Entry> </Entry>
And used it like this:
<control:RequiredEntry Placeholder="placehld" />
The TextColor and Style Attributes were never set (and the behavior was also not added).
The Placeholder was set though.

Any help here?
Either setting the style value and behavior in the constructor in codebehind or doing it all via custom xaml control would be fine.
Though i would prefer doing it in a xaml control since its what they are there for.

Thanks for any Input!

Greetings
Dennis

Answers

Sign In or Register to comment.