Change Entry border color from .cs file

Michal 41 Reputation points
2021-08-16T07:20:02.92+00:00

Hello,

iam pretty new in Xamarin so I'd like to ask you for help.
I need to change border color from .cs file - during validation email format for example.

My Entry:

public sealed class StandardEntry : Entry
    {
        public static BindableProperty CornerRadiusProperty =
            BindableProperty.Create(nameof(CornerRadius), typeof(int), typeof(StandardEntry), 0);

        public static BindableProperty BorderThicknessProperty =
            BindableProperty.Create(nameof(BorderThickness), typeof(int), typeof(StandardEntry), 0);

        public static BindableProperty PaddingProperty =
            BindableProperty.Create(nameof(Padding), typeof(Thickness), typeof(StandardEntry), new Thickness(5));

        public static BindableProperty BorderColorProperty =
            BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(StandardEntry), Color.Transparent);

        public int CornerRadius
        {
            get => (int)GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty, value);
        }

        public int BorderThickness
        {
            get => (int)GetValue(BorderThicknessProperty);
            set => SetValue(BorderThicknessProperty, value);
        }
        public Color BorderColor
        {
            get => (Color)GetValue(BorderColorProperty);
            set => SetValue(BorderColorProperty, value);
        }
        /// <summary>
        /// This property cannot be changed at runtime in iOS.
        /// </summary>
        public Thickness Padding
        {
            get => (Thickness)GetValue(PaddingProperty);
            set => SetValue(PaddingProperty, value);
        }
    }

But if I call myField.BorderColor = Color.Red; from .cs file it doesnt work.

How can i change color of this field programmatically please?

Thank you, M

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-08-17T02:08:55.693+00:00

    Hello @Michal ,​

    Welcome to our Microsoft Q&A platform!

    Xamarin.Forms.Entry doesn't provide the 'BorderColor' property directly. To change the entry's border color, try to perform the work on the native platform. You could use Custom Renderer for this function.

    Here is the sample code, you could refer to it.

    Android Platform

       [assembly:ExportRenderer(typeof(StandardEntry),typeof(CustomEntryRenderer))]  
       namespace TestApplication_6.Droid  
       {  
           public class CustomEntryRenderer : EntryRenderer  
           {  
               StandardEntry entry;  
               public CustomEntryRenderer(Context context) : base(context)  
               {  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
               {  
                   base.OnElementChanged(e);  
                   entry = Element as StandardEntry;  
               }  
         
               protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
               {  
                   base.OnElementPropertyChanged(sender, e);  
         
                   var nativeEditText = (EditText)Control;  
                   var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());  
                   shape.Paint.Color = entry.BorderColor.ToAndroid(); //entry.BorderColor is the 'BorderColor' property of the custom entry class  
                   shape.Paint.SetStyle(Paint.Style.Stroke);  
                   shape.Paint.StrokeWidth = 10;  
                   nativeEditText.Background = shape;  
               }  
           }  
       }  
    

    iOS platform

       [assembly: ExportRenderer(typeof(StandardEntry), typeof(CustomEntryRenderer))]  
       namespace TestApplication_6.iOS  
       {  
           public class CustomEntryRenderer : EntryRenderer  
           {  
               StandardEntry entry;  
               protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
               {  
                   base.OnElementChanged(e);  
                   entry = Element as StandardEntry;  
               }  
         
               protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
               {  
                   base.OnElementPropertyChanged(sender, e);  
                   Control.Layer.BorderColor = entry.BorderColor.ToCGColor();  
                   Control.Layer.BorderWidth = 2;  
               }  
           }  
       }  
    

    Then you could change the value of customized 'BorderColor' property to change the color of the entry in .cs file.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful