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>
Answers
What's the problem you're facing ? What exactly do you want to achieve ?
Add behaviors to the custom control that I can apply to the
Entry
.I wasn't able to find a way to populate the
IList<Behaviors>
of theEntry
inside the custom control by adding the behaviors in the custom control.Create behavior for your custom control , and find the Entry inside it ..
So you mean, that there is no way to add behaviors to my custom control like I do with an
Entry
?You mean, that if I have a behavior for an
Entry
and I need it also for my custom control then I cannot reuse it and I should create a different behavior doing the same thing but for my custom control?You mean that if I have another custom control with 2
Entries
(i.e. phone number with National prefix and the phone number) and I need the same behavior then I should create a third for that particular custom control?Why don't you use behavior with Entry in your custom control directly?
local:NumericValidationBehavior
is Behavior is for Entry , and it is reusable.I made the custom control in order to use it in multiple cases. Different cases need different behaviors.
As a result, I would like to add the behaviors that I want depending on the use of the custom control.
That is the reason that we can add behaviors to a simple
Entry
control depending on the situation.I suggest you handle the logic in custom renderer code behind.
What is the
if (A)
?Don't you think that this is a very cumbersome and unpractical solution that also needs a lot of maintenance?
Behavior A is created and used for Control A , you can't add Behavior of Entry on your custom control .
Did you ever find a solution for this?
@IeuanWalker ...not a good one… but I realized also that I made some mistakes.
Firstly, just to describe the solution, is to create a
bindable property
of type bool to specify if I want to attach thebehavior
(or theeffect
)On property change of the
bindable property
I add thebehavior
by code.It’s not a pretty solution. I can re-use the
behaviors
or theeffects
which is good but I need to create a fewbindable properties
… which is not good.The default value of the
bindable property
isfalse
so myXAML
is not that verbose. In my case I only had 2behaviors
and 1effect
so it worked for me.I think that one mistake that I did is that the
behaviors
(or theeffects
) should be for theContentView
that contains the entry instead of theentry
.I'm was searching to the same thing. My solution was:
1 - Create a List<Behavior> as a Bindable property inside the component
2- Name you entry in code behind
3- Enter the following code when creting the bindable property
4- Populate the list in the viewModel (with any behaviors you need) and binding to the component
Hi
can you share an example of how you did it, I am new to xamarin and I need to do this, thanks for your help