Search code examples
data-bindingxamarin.formscustom-controlsinotifypropertychangedcustom-renderer

Custom property of a custom control not being set - xamarin forms


I'm trying to create a custom entry control with EntryType property which I then use inside custom renderers to set platform specific values. But the EntryType is never set when used from Xaml. Here's my code:

public class ExtendedEntry : Xamarin.Forms.Entry
{

    public static readonly BindableProperty EntryTypeProperty = BindableProperty.Create(
        propertyName: "EntryType",
        returnType: typeof(int),
        declaringType: typeof(EntryTextType),
        defaultValue: 1
        );

    public EntryTextType EntryType
    {
        get
        {
            return (EntryTextType)GetValue(EntryTypeProperty);
        }
        set
        {
            SetValue(EntryTypeProperty, value);

        }
    }

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);

        if (propertyName == EntryTypeProperty.PropertyName)
        {

        }
    }
}

public enum EntryTextType
{
    Any,
    Numeric,
    Url,
    Email
}

public class ExtendedEntryRenderer : EntryRenderer
{
    public ExtendedEntryRenderer(Android.Content.Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            var element = (ExtendedEntry)Element;
            Control.Hint = element.Placeholder;
            switch(element.EntryType)
            {
                case EntryTextType.Numeric:
                    Control.SetRawInputType(Android.Text.InputTypes.ClassNumber);
                    break;
                default:
                    break;
            }
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var p = e.PropertyName;
        base.OnElementPropertyChanged(sender, e);
    }

}

And then in XAML, I use the control as this:

<controls:ExtendedEntry Placeholder="Password" IsPassword="True" Text="{Binding Secret}" EntryType="Any"/>

The problem is, EntryType never gets set to EntryTextType.Any, and always uses the default value of EntryTextType.Numeric. What am I missing here? Thanks.


Solution

  • I noticed some discrepancies in the EntryTypeProperty declaration.

    • Declaring type should be the owner which is ExtendedEntry
    • And, to instruct XAML to use the enum TypeConverter, you will have to define the datatype as EntryTextType

    So your new code will look like:

    public static readonly BindableProperty EntryTypeProperty = BindableProperty.Create(
        propertyName: "EntryType",
        returnType: typeof(EntryTextType),
        declaringType: typeof(ExtendedEntry),
        defaultValue: EntryTextType.Numeric
        );