Custom property of a custom control not being set

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.

Best Answer

  • PratikChandraPratikChandra US ✭✭
    Accepted Answer

    As answered here, https://stackoverflow.com/a/51770430/618220 , the fix is to use the exact type for default value property. So, instead of:

    defaultValue: 1
    

    we need to use:

    defaultValue: EntryTextType.Numeric
    

    Doing this instructs the XAML engine to use proper TypeConverter which then results in correct data binding.

Answers

  • SzbestaSzbesta Member ✭✭✭
                        case EntryTextType.Numeric:
                            Control.SetRawInputType(Android.Text.InputTypes.ClassNumber);
                            break;
                        default:
                            break;
    

    to me it looks like you are never handling the case of any other entrytype than numeric, but could be wrong there

  • PratikChandraPratikChandra USMember ✭✭

    @Szbesta yes, and that is because I don't need to, as I'm happy with the default for all other cases i.e. allowing any string to be typed inside textbox. The only scenario that I need to explicitly set is when I want a numeric textbox. The actual problem is, as shown in my xaml, even when I set EntryType = ANY, EntryTypeProperty never gets set to ANY.

  • PratikChandraPratikChandra USMember ✭✭
    Accepted Answer

    As answered here, https://stackoverflow.com/a/51770430/618220 , the fix is to use the exact type for default value property. So, instead of:

    defaultValue: 1
    

    we need to use:

    defaultValue: EntryTextType.Numeric
    

    Doing this instructs the XAML engine to use proper TypeConverter which then results in correct data binding.

Sign In or Register to comment.