Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Xamarin Forms how to add behaviors to custom control

$
0
0

I have created a custom control,which is a ContentView with a Label and an Entry

The xaml of the custom controls looks like this:

<Label Text="{Binding Source={x:Reference ValidationControl}, Path=Caption}"/>
<Entry Text="{Binding Source={x:Reference ValidationControl}, Path=Value, Mode=TwoWay}" />

The code behind of the custom control looks like this:

public static readonly BindableProperty CaptionProperty = BindableProperty.Create(
    nameof(Caption), typeof(string), typeof(ValidationEntry), default(string));

public string Caption
{
    get => (string)GetValue(CaptionProperty);
    set => SetValue(CaptionProperty, value);
}

public static readonly BindableProperty ValueProperty = BindableProperty.Create(
    nameof(Value), typeof(string), typeof(ValidationEntry), default(string));

public string Value
{
    get => (string)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

I’m using the custom control in the following way

<controls:ValidationEntry Caption=”Name:” Value="{Binding FullName, Mode=TwoWay}" />

My question is how to add behaviors to the custom control?
I would like to add them in the place that I’m using the control. i.e.

<controls:ValidationEntry Caption="Name:"
                          Value="{Binding FullName, Mode=TwoWay}">
    <controls:ValidationEntry.EntryBehaviors>
        <behaviors:EntryLengthValidatorBehavior IgnoreSpaces="True"/>
    </controls:ValidationEntry.EntryBehaviors>
</controls:ValidationEntry>

Viewing all articles
Browse latest Browse all 89864

Trending Articles



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