locked
The return value is always false even the setter is true RRS feed

  • Question

  • User393891 posted
    namespace TravelRecord
    {
        public class ValidatableObject<T> : ExtendedBindableObject, IValidity, INotifyPropertyChanged
        {
    
            public bool IsValid { get; set; }
            public T Value { get; set; }
    
        }
    }
    

    View Model

    public class RegisterViewModel : BaseViewModel
    {
        public ValidatableObject<string> FirstName { get; set; } 
    }
    

    View

    <local:RoundedEntry Placeholder="First name" WidthRequest="100" Style="{StaticResource RoundedEntryStyle}"
                        Text="{Binding FirstName.Value }">
        <local:RoundedEntry.Behaviors>
            <local:EntryEmptyValidationBehaviour User="{Binding Source={x:Reference page}, Path=BindingContext.FirstName, Mode=TwoWay}" />
        </local:RoundedEntry.Behaviors>
    </local:RoundedEntry>
    

    Behavior

    namespace TravelRecord
    {
        public class EntryEmptyValidationBehaviour : BehaviorBase<RoundedEntry>
        {
    
            RoundedEntry control;
            string _placeHolder;
            Color _placeHolderColor;
    
            static ValidatableObject<string> user;
    
            public static readonly BindableProperty UserProperty =
            BindableProperty.Create(
            nameof(User),
            typeof(ValidatableObject<string>),
            typeof(EntryEmptyValidationBehaviour),
            null,
            BindingMode.TwoWay,
            propertyChanged: (bindableObject, oldValue, newValue) =>
            {
                user =  (ValidatableObject<string>)newValue;
            });
            public ValidatableObject<string> User {
                get => (ValidatableObject<string>)GetValue(UserProperty); 
                set => SetValue(UserProperty, value); 
            }
    
            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)
            {
                var control = (RoundedEntry)sender;
                var NotEmpty = !string.IsNullOrEmpty(e.NewTextValue);
                control.Placeholder = NotEmpty ? _placeHolder : "Required";
                control.PlaceholderColor = NotEmpty ? _placeHolderColor : Color.Red;
                user.IsValid = NotEmpty ? true : false;
    
            }
    
        }
    }
    
    Sunday, April 26, 2020 4:34 PM

Answers

  • User382871 posted

    The return value is always false even the setter is true Which value is the 'return value'? 'NotEmpty' or 'IsValid'? Could you posted more details about the issue. And it's difficult to reproduce the issue with poseted code.

    For the user.IsValid = NotEmpty ? true : false line, using the following command will be enough. user.IsValid = NotEmpty;

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 27, 2020 9:09 AM

All replies

  • User382871 posted

    The return value is always false even the setter is true Which value is the 'return value'? 'NotEmpty' or 'IsValid'? Could you posted more details about the issue. And it's difficult to reproduce the issue with poseted code.

    For the user.IsValid = NotEmpty ? true : false line, using the following command will be enough. user.IsValid = NotEmpty;

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 27, 2020 9:09 AM
  • User393891 posted

    Thanx @Jarvan , I continued writing the code, but when i checked the code, i couldn't replicate the error

    Monday, April 27, 2020 11:50 AM