Search code examples
c#.netmvvmmvvm-light

What is the importance of RaisePropertyChanged?


I am currently learning MVVM and i don't understand how RaisePropertyChanged is important

For example this function :

public City SelectedCity
{
    get
    {
        return selectedcity;
    }
    set
    {
        RaisePropertyChanging(SelectedCityLocationPropertyName);
        selectedtrend = value;
        RaisePropertyChanged(SelectedCityLocationPropertyName);
        MessageBox.Show(City.Name);
    }
}

and this one :

public City SelectedCity
{
    get
    {
        return selectedcity;
    }
    set
    {
        //   RaisePropertyChanging(SelectedCityLocationPropertyName);
        selectedtrend = value;
        //   RaisePropertyChanged(SelectedCityLocationPropertyName);
        MessageBox.Show(City.Name);
    }
}

Give exactly the same result for me. Can you please tell me why is RaisePropertyChanged so important and give me an example where it would make a vital difference?


Solution

  • Read this:

    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

    The RaisePropertyChanging event is used to notify UI or bound elements that the data has changed. For example a TextBox needs to receive a notification when the underlying data changes, so that it can update the text you see in the UI. Without the NotifyPropertyChanged event, the TextBox would have no idea that the data changed.

    It's very important in MVVM.