8
votes

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.

1
Looks like you're going down a good path. I see you're implementing validation / INotifyPropertyChanged and stuff. Maybe you want to check out Csla.Net, which will handle this stuff for you. It provides a framework on which to build great models, as well as offers some help for the various UIs it supports (basically all of them). lhotka.net/cslanetAndy
Thanks for your suggestion, I'll take a look at it ;-)zapico

1 Answers

16
votes

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