Search code examples
c#xamarinprism

How to hide the error label in Xamarin forms, when the Entry field is not visible?


I am trying to create a Login page that has validation errors. Right now the validation errors are also appearing if the Entry field is not visible. How would I hide the error labels, when the entry field is not visible? As shown below: The PIN entry field is invisible on the login page but the error message: Pin is required, highlights. Please could anyone suggest a workaround?

enter image description here


Solution

  • Agree with Jason . You can use the data-binding to binding the IsVisible of label to the property in your viewmodel.

    <Label Text="Pin is required!"  TextColor="Red"  HorizontalTextAlignment="Center" IsVisible="{Binding isVisible}"/>
    
    <Button Text="sign in" BackgroundColor="Red" TextColor="White" Command="{Binding ClickCommand}"  WidthRequest="200" />
    

    in your ViewModel

    public class YourViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public ICammand ClickCommand {get; set;}
    
    
        private bool isvisible;
    
        public bool isVisible
        {
         get
         {
            return isvisible;
         }
    
         set
         {
          if (isvisible!= value)
          {
            isvisible= value;
            NotifyPropertyChanged();
          }
        }
    
    
        public YourViewModel()
        {
            //... 
            isVisible = true; //show the label in default
             
            ClickCommand = new Command(() =>
            {
               if(xxx)
               {
                  isVisible =false;
               }
               
               else
               { 
                  isVisible =true;
               }
            }) ;
    
        }
    
    }