Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM Model to ViewModel communication

I have a simple scenario with a View, a ViewModel and a custom type class.

The model class is something like:

public class Person : Validation.DataError, INotifyPropertyChanged
{
    #region INotifyProperty

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion


    public global::System.String name
    {
        get
        {
            return _name;
        }
        set
        {
            _name= value;
            RaisePropertyChanged("name");
        }
    }
    private global::System.String _name;

}

In the ViewModel I have a Person property:

private Model.Person person;

        public Model.Person Person
        {
            get
            {
                return person;
            }
            set 
            {
                this.person= value;

                this.RaisePropertyChanged("Person");
                this.SavePersonCommand.OnCanExecuteChanged();
            }
        }

In my View I have a textbox that is bound to Person.name

So the ViewModel is not executing the set method because the Person object is still the same... it is executing the set method in the Model property.

I want to let the user change the person name and make a call to another method (search through a web service and other stuff...) and I think this functionality should be in the ViewModel.

I'm using Messenger from MVVM Light toolkit to communicate between different viewmodels and between views and viewmodels.

Now I don't know if I should use a mediator too for this or if I should know another way to solve this.

like image 674
zapico Avatar asked Apr 28 '11 16:04

zapico


People also ask

How do you communicate between view and ViewModel?

What is a proper way to communicate between the ViewModel and the View , Google architecture components give use LiveData in which the view subscribes to the changes and update itself accordingly, but this communication not suitable for single events, for example show message, show progress, hide progress etc.

How communication happens in MVVM?

Messages are often packaged into different types, so a ViewModel can subscribe to only listen for specific message types such as CloseApplicationMessages. This kind of loosely coupled event system is ideal for MVVM because the ViewModels don't need to know about each other to be able to do their job.

What is the role of ViewModel in MVVM?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.


1 Answers

Just subscribe to the PropertyChanged event of the Person in your ViewModel and check for the "Name" property, or whatever you wanna do:

Person.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Person_PropertyChanged);

void Person_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if(e.PropertyName == "Name")
    {
         //do something
    }
}
like image 76
lukebuehler Avatar answered Oct 18 '22 10:10

lukebuehler