I have a custom behavior for Entry. It has a bindable property MobileCountryCode.
public class MobileNumberValidation:Behavior<Entry>
{
public static readonly BindableProperty MobileCountryCodeProperty =
BindableProperty.Create(nameof(MobileCountryCode)
,typeof(string),typeof(MobileNumberValidation)
,defaultValue:String.Empty);
public string MobileCountryCode
{
get=> (string)GetValue(MobileCountryCodeProperty);
set => SetValue(MobileCountryCodeProperty, value);
}
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
this.BindingContext = bindable.BindingContext;
bindable.Unfocused += Bindable_Unfocused;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.Unfocused -= Bindable_Unfocused;
}
private void Bindable_Unfocused(object sender, FocusEventArgs e)
{
// Validation logic
}
}
I have used this custom behavior in xaml with a hardcoded string value to the property 'MobileCountryCode' and it works just fine.
<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
behaviors:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference ent_name}"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
InValidStyle="{x:StaticResource InvalidEntry}"
MobileCountryCode="+1" />
</Entry.Behaviors>
</Entry>
<!--this is all good-->
But when I change the value form hardcoded string to a Binding it gives an error XFC0009: No property, BindableProperty, or event found for "CodeInString", or mismatching type between value and property.
<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
behaviors:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference ent_name}"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
InValidStyle="{x:StaticResource InvalidEntry}"
MobileCountryCode="{Binding CodeInString}" />
</Entry.Behaviors>
</Entry>
<!--this gives error-->
CodeInString
is a property in the page's View model.
public string CodeInString {get; set;} = "+22"
How can this issue be resolved??
I have gone through other similar question here but none of the solution worked for me. The same value can be binded to Label Text and Entry Text and it runs without any issue and give the correct value. I have binded it to all the other string Properties of its VM and still no luck.
Edit: I have added an extra bindable property IsInValid of type bool. This also has the same issue with binding to the viewmodel property.
public class MobileNumberValidation:Behavior<Entry>
{
private static readonly BindableProperty IsInValidProperty =
BindableProperty.Create(nameof(IsInValid),typeof(bool),typeof(MobileNumberValidation),defaultValue:false);
public bool IsInValid
{
get=>(bool)GetValue(IsInValidProperty);
set => SetValue(IsInValidProperty, value);
}
public static readonly BindableProperty MobileCountryCodeProperty =
BindableProperty.Create(nameof(MobileCountryCode)
,typeof(string),typeof(MobileNumberValidation)
,defaultValue:String.Empty);
public string MobileCountryCode
{
get=> (string)GetValue(MobileCountryCodeProperty);
set => SetValue(MobileCountryCodeProperty, value);
}
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.Unfocused += Bindable_Unfocused;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.Unfocused -= Bindable_Unfocused;
}
private void Bindable_Unfocused(object sender, FocusEventArgs e)
{
// Validation logic
}
}
The Xaml part
<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="{Binding MobileNo}">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
BindingContext="{Binding Path=BindingContext, Source={Reference thisPage}}"
IsInValid="{Binding IsMobileNoValid}"
MobileCountryCode="{Binding MobileCountryCodeSelected.MobileCode}" />
</Entry.Behaviors>
</Entry>
IsMobileNoValid is a boolean property in the pages ViewModel.
But on binding this to the IsInValid property of customBehaviour:MobileNumberValidation it gives the same error as before but for the IsInValid property. This works with hard coded boolean values (IsInValid="False" ,this works). The MobileCountryCode is working fine now, there is no error for this.