Jeppe Christensen
Jeppe Christensen

Reputation: 1890

CustomRenderer to dynamically change border color

I have an Entry, which I want to add a red border around when a button is pressed if the entry is empty. Therefore I need to be able to change the color dynamically. (standard validator)

xaml:

<local:BorderChange Placeholder="Example Entry" BorderColor="#ff4444"></local:BorderChange>

PCL Control:

namespace Project
{
    public class BorderChange : Entry
    {
        public string BorderColor
        {
            get; 
            set;
        }
    }
}

iOS Customrenderer:

[assembly: ExportRenderer(typeof(BorderChange), typeof(BorderColorChange))]
namespace Project.iOS
{
    public class BorderColorChange : EntryRenderer
    {
    //init color

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

            if(Control != null)
            {
                Control.Layer.BorderColor = UIColor.Blue; //This is where i want to add my color
            }
        }
    }
} 

Upvotes: 0

Views: 690

Answers (1)

Valeriy Kovalenko
Valeriy Kovalenko

Reputation: 165

A better approach for such needs is to use Effects.

Here is a well-described example of how to create an effect with parameters as attached properties. You'll be able to bind a property from your view model (Let's say IsValid directly to the attached property of the effect).

Upvotes: 0

Related Questions