question

abanoubadly-0688 avatar image
0 Votes"
abanoubadly-0688 asked LeonLu-MSFT commented

Xamarin forms behaviors

I'm working on a project that uses a lot of User Inputs.
I have an input screen like this
145929-img-20211103-062504.jpg


but i want to use behaviors to show labels with error messages like this

145987-img-20211103-062526.jpg



I've managed to achieve this without behaviors but it took a lot of code,
so I want to use behaviors to have more of a clean robust code.
I've watched a lot of tutorials that changes the entry text or color,
but as I mentioned above I'm trying to have error labels.
am not even sure that is possiple or not.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered abanoubadly-0688 commented

I think you can use triggers to set label IsVisible property
understanding-triggers-in-xamarin-forms


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@alessandrocaliaro
it's not really working in my case am trying to achieve this using View Model,
so I cannot really manipulate the binding .

0 Votes 0 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

I am trying to bind the view property to th view model to validate it and change the UI based on the result.

If you want to acheve it with binding. I give write a ViewModel to judge the Name part, other part(Email, Password), you can create property like Name property. You can judge the Name property if is null in the OnPropertyChanged method. if the Name is not null to set the NameErrorVisibility to false.

public class MyNewViewModel: INotifyPropertyChanged
    {
        public MyNewViewModel()
        {
            NameError = "Name is Required";
        }
        string name;
        public string Name
        {
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");

                }
            }
            get
            {
                return name;
            }
        }

        bool _nameErrorVisibility = true;
        public bool NameErrorVisibility
        {
            get
            {
                return _nameErrorVisibility;
            }

            set
            {
                if (_nameErrorVisibility != value)
                {
                    _nameErrorVisibility = value;
                    OnPropertyChanged("NameErrorVisibility");

                }
            }

        }


        public string NameError { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {

            if (propertyName == nameof(Name))
            {
                //update controls here
               if (Name.Length == 0|| Name==null)
                {

                    NameErrorVisibility= true;
                }
                else
                {

                    NameErrorVisibility = false;
                };
            }
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }


    }


For testing, you can copy your xaml code in new page, then copy my above MyNewViewModel.cs, binding it in the new pages' constructor.

public partial class Page4 : ContentPage
    {
        public Page4()
        {
            InitializeComponent();
            BindingContext = new MyNewViewModel();
        }
    }


Best Regards,

Leon Lu



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@abanoubadly-0688 trying to achieve this using View Model, so I cannot really manipulate the binding .

If you want to achieve it by data-binding, you could extend the INotifyPropertyChanged interface like this thread :https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-mode#viewmodels-and-property-change-notifications, And I find a similar thread about your issue, you can check it.https://stackoverflow.com/questions/49006331/validation-in-xamarin-using-dataannotation

0 Votes 0 ·

@abanoubadly-0688 Please see my above editted answer.

0 Votes 0 ·
abanoubadly-0688 avatar image
0 Votes"
abanoubadly-0688 answered

@LeonLaude
1) am trying to use data trigger to bind behavior between the entry and its error label
2) am trying to bind the view property to th view model to validate it and change the UI based on the result.
if it help that is my xaml code

 <!--Name-->
             <Entry
                 Placeholder="{xct:Translate Name}"
                 Text="{Binding Name}"
                 Style="{StaticResource EntryStyle}"/>
    
             <!--Name Error-->
             <Label
                 Text="{Binding NameError}"
                 Style="{StaticResource ErrorLabel}"
                 IsVisible="{Binding NameErrorVisibility}"
                 />
    
             <!--Email-->
             <Entry
                 Placeholder="{xct:Translate Email}"
                 Text="{Binding Email}"
                 Style="{StaticResource EntryStyle}"/>
    
             <!--Email Error-->
             <Label
                 Style="{StaticResource ErrorLabel}"
                 Text="{Binding EmailError}"
                 IsVisible="{Binding EmailErrorVisibility}"
                 />
    
             <!--Password-->
             <Entry
                 Placeholder="{xct:Translate Password}"
                 Text="{Binding Password}"
                 Style="{StaticResource EntryStyle}"/>
    
             <!--Password Error-->
             <Label
                 Style="{StaticResource ErrorLabel}"
                 Text="{Binding PasswordError}"
                 IsVisible="{Binding PasswordErrorVisibility}"
                 />
    
             <!--Confirmed Password-->
             <Entry
                 Placeholder="{xct:Translate ConfirmedPassword}"
                 Text="{Binding ConfirmedPass}"
                 Style="{StaticResource EntryStyle}"/>
    
             <!--Confirmed Password Error-->
             <Label
                 Style="{StaticResource ErrorLabel}"
                 Text="{Binding ConfirmedPassError}"
                 IsVisible="{Binding ConfirmedPassErrorVisibility}"
                 />
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.