How to Invoke PropertyChanged() to internal item when external item changed and viceversa

Radoslav Atanasov 26 Reputation points
2021-01-04T19:39:56.913+00:00

I have class Student that inherit INotifyPropertyChanged and there is a string property Name:

public string Name
{
get => name;
set => SetProperty(ref text, value);
}

I also have class StudentViewModel that inherit INotifyPropertyChanged and there is bool property IsEnabled, and Student property Student.

So in my .xaml file I have data binding between and .

So if I have to notify internal property in StudentViewModel class when IsEnabled change I always use this method:

public bool IsEnabled
    {
        get => _isEnabled;
        set
        {

            SetProperty(ref text, value);
            OnPropertyChanged(nameof("some internal property name in StudentViewModel"));
        }
    }

But now I want to notify property IsEnbaled when Student.Name is change when the Editor text is empty for ex. , but I cannot write something like this:
OnPropertyChanged(nameof(IsEnbaled)); into the setter of Name prop in my Student class.

And I also cannot write something like this:
OnPropertyChanged(nameof(Student.Name)); into the setter of IsEnbaled prop in my StudentViewModel class.

I will be very grateful for any advice

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-01-05T02:48:43.273+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I want to notify property IsEnbaled when Student.Name is change when the Editor text is empty for ex.

    You can use PropertyChanged event to notify IsEnabled property in StudentViewModel when Student.Name is changed like following code.

       public class StudentViewModel: INotifyPropertyChanged  
           {  
               public Student student { get; set; }  
               bool _isEnabled = false;  
               public bool IsEnabled  
               {  
                   get  
                   {  
                       return _isEnabled;  
                   }  
         
                   set  
                   {  
                       if (_isEnabled != value)  
                       {  
                           _isEnabled = value;  
                           
                           OnPropertyChanged("IsEnabled");  
         
                       }  
                   }  
         
               }  
         
               public StudentViewModel()  
               {  
                   student = new Student() { Name="test1" };  
                   student.PropertyChanged += Student_PropertyChanged;  
               }  
         
               private void Student_PropertyChanged(object sender, PropertyChangedEventArgs e)  
               {  
                   
         
                   if (e.PropertyName == "Name")  
                   {  
                       IsEnabled = true;  
                   }  
                     
                   
               }  
         
               public event PropertyChangedEventHandler PropertyChanged;  
         
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
         
           }  
    

    I make an test with Entry and Editor, When I change the text in the Entry, Editor's IsEnabled will be changed from false to true.

       <Entry Text="{Binding student.Name}"></Entry>  
                 
               <Editor Text="Enditor" IsEnabled="{Binding IsEnabled}"></Editor>  
    

    53435-image.png

    53436-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2021-01-09T13:50:28.777+00:00

    @Radoslav Atanasov I have done some tests with Fody.

       public class StudentViewModel : INotifyPropertyChanged  
        {  
      
            public bool IsEnabled { get; set; }  
            public Student Student { get; set; } = new Student();  
      
            public StudentViewModel()  
            {  
                Student.PropertyChanged += Student_PropertyChanged;  
      
                ShowValueCommand = new Command(async () =>  
                {  
                    await Application.Current.MainPage.DisplayAlert("Value", Student.Name, "Ok");  
                });  
            }  
      
            private void Student_PropertyChanged(object sender, PropertyChangedEventArgs e)  
            {  
                IsEnabled = !string.IsNullOrEmpty( Student.Name);  
            }  
      
            public ICommand ShowValueCommand { get; protected set; }  
      
            public event PropertyChangedEventHandler PropertyChanged;  
        }  
    
    
    public class Student : INotifyPropertyChanged  
    {  
        public string Name { get; set; }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
    }  
    

    This works for me

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.