Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Custom property of a custom control not being set

$
0
0

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.


Viewing all articles
Browse latest Browse all 77050

Trending Articles