Custom control behavior

Ángel Rubén Valdeolmos Carmona 611 Reputation points
2021-05-31T15:00:07.34+00:00

I have a custom control that has an entry among others, to this control I have put this property:

public static readonly BindableProperty EntryBehaviorsProperty = BindableProperty.Create (nameof (Behaviors), typeof (List <Behavior <Entry>>), typeof (List <Behavior <Entry>>), null, BindingMode.OneTime, propertyChanged: OnBehaviorChanged);

 private static void OnBehaviorChanged (BindableObject bindable, object oldValue, object newValue)
 {
     var view = bindable as EntryBorder;
     if (view.Behaviors! = null && view.Behaviors.Count> 0)
     {
         foreach (var item in view.Behaviors)
         {
              //x:name entry
             view.entryBorder.Behaviors.Add (item);
         }
     }
 }

 public new IList <Behavior> Behaviors
     {
         get => (IList <Behavior>) GetValue (EntryBehaviorsProperty);
         set => SetValue (EntryBehaviorsProperty, value);
     }



  <controls:EntryBorder Style="{StaticResource GeneralEntryClear}" HorizontalOptions="FillAndExpand"
                              Text="{Binding PersonCustomerRequest.PersonaCliente.Nombre}" 
                         >

            <controls:EntryBorder.Behaviors>
                <xct:EmailValidationBehavior x:Name="hola" Flags="ValidateOnUnfocusing" />
            </controls:EntryBorder.Behaviors>
        </controls:EntryBorder>

        <Label Text="Invalid email"
           TextColor="Red"
           FontSize="10"
           IsVisible="{Binding IsNotValid, Source={x:Reference hola}}" />

this doesn't work for me

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-06-02T09:46:17.167+00:00

    Hi Angelru-4290,

    Welcome to our Microsoft Q&A platform!

    I mean you can try another alternative, which is to create a separate Property for each behavior.

    public static readonly BindableProperty MaxLengthReachedBehaviorProperty = BindableProperty.Create(nameof(EntryBehavior), typeof(EntryBehavior), typeof(MaxLengthReachedBehavior), null, BindingMode.OneTime);  
      
    public MaxLengthReachedBehavior EntryBehavior  
    {  
        get => (MaxLengthReachedBehavior)GetValue(MaxLengthReachedBehaviorProperty);  
        set => SetValue(MaxLengthReachedBehaviorProperty, value);  
    }  
    

    add behavior,

    private void AddBehavior()  
    {  
        MaxLengthReachedBehavior maxBehavior = new MaxLengthReachedBehavior();  
        maxBehavior.MaxLengthReached += MaxBehavior_MaxLengthReached;  
        testEntry.MaxLength = 10;  
        testEntry.EntryBehavior = maxBehavior;  
    }  
      
    private void MaxBehavior_MaxLengthReached(object sender, MaxLengthReachedEventArgs e)  
    {  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.