Forum Xamarin.Forms

How to add Behaviors property to ContentView element

I have a ContentView element that I want to add Behaviors to. Basically I have a custom element that is an entry with a few other elements add to it and I want to be able to pass Behaviors to the ContentView element and have that pass them along to the Entry. Anyone know how I can accomplish this or how to add Behaviors to an element.

I looked through the Xamarin.Forms code and found this code, but it says AttachedCollection is inaccessible due to its protection level:

internal static readonly BindablePropertyKey BehaviorsPropertyKey = BindableProperty.CreateReadOnly("Behaviors", typeof(IList<Behavior>), typeof(VisualElement), default(IList<Behavior>),
defaultValueCreator: bindable =>
{
    var collection = new AttachedCollection<Behavior>();
    collection.AttachTo(bindable);
    return collection;
});

public static readonly BindableProperty BehaviorsProperty = BehaviorsPropertyKey.BindableProperty;

public IList<Behavior> Behaviors
{
    get { return (IList<Behavior>)GetValue(BehaviorsProperty); }
}

This is what i'd like to be able to do:

<ctrls:ExtendedEntry Text="{Binding Account.name, Mode=TwoWay}" HorizontalOptions="FillAndExpand">
    <ctrls:ExtendedEntry.Behaviors>
        <local:MaxLengthValidationBehavior MaxLength="18" x:Name="nameValidator" />
    </ctrls:ExtendedEntry.Behaviors>
</ctrls:ExtendedEntry>

Answers

  • dinobdinob USUniversity ✭✭✭

    I am looking for similar. I have ContentView and I would like to add to it KeyDown/KeyUp kind of events/behaviors. Basically, capture typed keys on Page / ContentView level and enter them into an Entry in the ContentView

Sign In or Register to comment.