Open CostasAthan opened 1 year ago
I think you are not properly binding RequiredString
.
At the moment you are setting RequiredString
with the current value of myLayout.Children.OfType<Entry>().ElementAt(1).Text
.
You should do something like this:
requiredStringValidationBehaviorReference
.SetBinding(
RequiredStringValidationBehavior.RequiredStringProperty,
new Binding("Text", source: myLayout.Children.OfType<Entry>().ElementAt(1))
);
For reference: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings#bindings-without-a-binding-context
At the moment you are setting RequiredString with the current value of myLayout.Children.OfType
().ElementAt(1).Text
Yes. Isn't RequiredString
the string that will be compared to the value provided in the third entry? When both the second and the third entries have as a value exactly the same string, shouldn't the above code return a valid state for the third one?
When you are executing this?
myLayout.Children.OfType<Entry>().ElementAt(2).Behaviors.Add
(
new RequiredStringValidationBehavior
{
RequiredString = myLayout.Children.OfType<Entry>().ElementAt(1).Text,
InvalidStyle = invalidEntryStyle,
Flags = ValidationFlags.ValidateOnValueChanging
}
);
myLayout.Children.OfType<Entry>().ElementAt(1).Text
is already set with the desired value? Otherwise, you are setting RequiredString
to string.Empty
, and since you are not binding the value, even if you change the text of the second entry, RequiredString
will remain string.Empty
.
@FedericoNembrini
Yes, I got what you are saying!
Should the following version work though?
myLayout.Children.OfType<Entry>().ElementAt(2).Behaviors.Add
(
new RequiredStringValidationBehavior
{
RequiredString = myLayout.Children.OfType<Entry>().ElementAt(1).Text,
InvalidStyle = invalidEntryStyle,
Flags = ValidationFlags.ValidateOnValueChanging,
BindingContext = myLayout.Children.OfType<Entry>().ElementAt(1).Text
}
)
It doesn't work either.
No, it cannot work. You have to set the binding with .SetBinding()
What's the purpose of BindingContext
property then?
You need to learn more about Binding and BindingContext.
In simple terms the BindingContext
is the object it is associated with. However, the fact remains that to tell RequiredString
what property to "Bind" to, you must use SetBinding()
.
Description
RequiredStringValidationBehavior stays always invalid
Steps to Reproduce
Expected Behavior
Actual Behavior
The RequiredStringValidationBehavior stays invalid
Basic Information