question

Angelru-4290 avatar image
0 Votes"
Angelru-4290 asked KyleWang-MSFT edited

Custom control behavior

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


dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Angelru-4290 What does "this doesn't work for me" mean? Did you get any errors when you compiled it? And to reproduce the issue, it wolud be better if you can provide a simple example via OneDrive or Github. Besides, about adding behavior to control, you can refer to this demo.

0 Votes 0 ·

I know how to add a behavior to the control, but if you see in my example, I don't want to add it to the control, if not to the entry that is in my custom control

0 Votes 0 ·

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT edited

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks, is what i ended up doing

0 Votes 0 ·