Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Custom Entry propert not reflected to child behavior

SherifAwadSherifAwad Member ✭✭

I have a view page with multiple same entry associated with the same behavior. So, I create a custom separate view and insert it to the view page.

<?xml version="1.0" encoding="UTF-8"?>
<local:RoundedEntry xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:TravelRecord"
             WidthRequest="100"
             Style="{StaticResource RoundedEntryStyle}"
             x:Class="TravelRecord.CustomEntryCell">

    <local:RoundedEntry.Behaviors>
        <local:EntryEmptyValidationBehaviour />
    </local:RoundedEntry.Behaviors>

</local:RoundedEntry>

The page

<local:CustomEntryCell Placeholder="First Name" Text="{Binding User.FirstName.Value}" />

<local:CustomEntryCell Placeholder="Last name" Text="{Binding User.LastName.Value}" />

<local:CustomEntryCell Placeholder="User name" Text="{Binding User.Username.Value}" />

But, fortunately the placeholder name doesn't reflected to the child behavior (always null)

protected override void OnAttachedTo(RoundedEntry bindable)
{

    control = bindable;
    _placeHolder = bindable.Placeholder;
    _placeHolderColor = bindable.PlaceholderColor;
    bindable.TextChanged += OnEntryTextChanged;
}

_placeHolder is empty even it's there in the view

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 28

    Hey friend, I couldn't produce this question according to code you post.

    What's the code RoundedEntry, CustomEntryCell and EntryEmptyValidationBehaviour ? Could you please post more code snippets or a basic demo so that we can try to reproduce this question on our side?

  • SherifAwadSherifAwad Member ✭✭

    RoundedEntry

        public class RoundedEntry : Entry
        {
            public static readonly BindableProperty BorderColorProperty =
                BindableProperty.Create(nameof(BorderColor),
                    typeof(Color), typeof(RoundedEntry), Color.Gray);
    
            public Color BorderColor
            {
                get => (Color)GetValue(BorderColorProperty);
                set => SetValue(BorderColorProperty, value);
            }
    
            public static readonly BindableProperty BorderWidthProperty =
                BindableProperty.Create(nameof(BorderWidth), typeof(int),
                    typeof(RoundedEntry));
    
            public int BorderWidth
            {
                get => (int)GetValue(BorderWidthProperty);
                set => SetValue(BorderWidthProperty, value);
            }
            public static readonly BindableProperty CornerRadiusProperty =
                BindableProperty.Create(nameof(CornerRadius),
                    typeof(double), typeof(RoundedEntry));
    
            public double CornerRadius
            {
                get => (double)GetValue(CornerRadiusProperty);
                set => SetValue(CornerRadiusProperty, value);
            }
            public static readonly BindableProperty IsCurvedCornersEnabledProperty =
                BindableProperty.Create(nameof(IsCurvedCornersEnabled),
                    typeof(bool), typeof(RoundedEntry), true);
    
            public bool IsCurvedCornersEnabled
            {
                get => (bool)GetValue(IsCurvedCornersEnabledProperty);
                set => SetValue(IsCurvedCornersEnabledProperty, value);
            }
    
        }
    

    EntryEmptyValidationBehaviour

        public class EntryEmptyValidationBehaviour : BehaviorBase<RoundedEntry>
        {
    
            RoundedEntry control;
            string _placeHolder;
            Color _placeHolderColor;
    
            protected override void OnAttachedTo(RoundedEntry bindable)
            {
    
                control = bindable;
                _placeHolder = bindable.Placeholder;
                _placeHolderColor = bindable.PlaceholderColor;
                bindable.TextChanged += OnEntryTextChanged;
            }
    
            protected override void OnDetachingFrom(RoundedEntry bindable)
            {
                bindable.TextChanged -= OnEntryTextChanged;
            }
    
    
            void OnEntryTextChanged(object sender, TextChangedEventArgs e)
            {
                if (control == null)
                    return;
                var NotEmpty = !string.IsNullOrEmpty(e.NewTextValue);
                control.Placeholder = NotEmpty ? _placeHolder : $"{_placeHolder} is required";
                control.PlaceholderColor = NotEmpty ? _placeHolderColor : Color.Red;
    
            }
        }
    

    CustomEntryCell is there in the question.

  • jezhjezh Member, Xamarin Team Xamurai

    But, fortunately the placeholder name doesn't reflected to the child behavior (always null)

    I tested your code, it's not true that the Placeholder property is not been reflected to the child behavior.
    It was your code handling errors that caused the problem.

    You can try to comment out the following code. the placeholder will show normally.

     void OnEntryTextChanged(object sender, TextChangedEventArgs e)
            {
                //if (control == null)
                //    return;
                //var NotEmpty = !string.IsNullOrEmpty(e.NewTextValue);
                //control.Placeholder = NotEmpty ? _placeHolder : $"{_placeHolder} is required";
                //control.PlaceholderColor = NotEmpty ? _placeHolderColor : Color.Red;
            }
    

    We should know that paramter e.NewTextValue of following code is just the actual text of this Entry, not the placeholder text.

    var NotEmpty = !string.IsNullOrEmpty(e.NewTextValue);
    
  • SherifAwadSherifAwad Member ✭✭

    The value is overwritten. so, I saved the first value to be used later.

  • jezhjezh Member, Xamarin Team Xamurai

    Do you mean we couldn't get the value of _placeHolder of this entry?

  • SherifAwadSherifAwad Member ✭✭

    There are two ways to get _placeHolder :
    1- Using the the bindable in OnAttachedTo => But the placeHolder value is null.
    2- Using sender in OnEntryTextChanged => But I overwrite it according to the not empty property.

    SO, I stored the first value and used it later.

    The question was why the value in the first method is null

    protected override void OnAttachedTo(RoundedEntry bindable)
    {
    
        _placeHolder = bindable.Placeholder; // Why null?
        _placeHolderColor = bindable.PlaceholderColor;
        bindable.TextChanged += OnEntryTextChanged;
    }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Sorry, because of the labor holiday, I just saw your message. I will check this question, and come back asap.

Sign In or Register to comment.