Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Like we notify the View about the changes happened to the ViewModel members with INotifyPropertyChanged, what is the normal way to notify the changes in the Model to the ViewModel?
Posted

You can implement the INotifyPropertyChanged interface also for the model.

 
Share this answer
 
Thanks Shmuel Zang for your reply.

I have implemented the INotifyPropertyChanged in the model and subscribed the PropertyChanged event of the model in my ViewModel. It works.

As we know that by applying [CallerMemberName] attribute (used in OnPropertyChanged()), we avoid the "Rename Refactoring doesn't change the String values" problem. I would like to know that is there any mechanism like [CallerMemberName] attribute to avoid the same problem ("Rename Refactoring doesn't change the String values") which may happen in the myModel_PropertyChanged() event handler in my ViewModel?
(exaclty here in the code: if (e.PropertyName == "MyFirstName"))

Here is my code:

C#
internal class Model:INotifyPropertyChanged
    {
        public Model()
        {
        }

        string _FirstName = "Shahir";
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                _FirstName = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if(PropertyChanged!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

C#
internal class MyViewModel:INotifyPropertyChanged
    {
        private Model myModel;

        public MyViewModel(Model model)
        {
            this.myModel = model;
            myModel.PropertyChanged += myModel_PropertyChanged;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        string _FirstName;
        public string MyFirstName
        {
            get { return myModel.FirstName; }
            set
            {
                _FirstName = value;
                OnPropertyChanged();
            }
        }

        private void myModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FirstName")
            {
                MyFirstName = myModel.FirstName;
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900