Click here to Skip to main content
14,299,664 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can implement the INotifyPropertyChanged interface also for the model.

   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:

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));
            }
        }
    }

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;
            }
        }
    }
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100