C# - How RaisePropertyChanged works in MVVM light

less than 1 minute read

MVVM light provides an easy way of the strong typed RaisePropertyChanged with lambda. The following code shows how this can find the correct string for member of ViewModel.

1
2
3
4
public void RaisePropertyChanged<T>(Expression<Func<T>> property)
{
    PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name);
}

so you can use the following code safely.

1
2
3
4
5
6
7
8
9
10
11
12
13
public class ViewModel : ViewModelBase
{
    private string _name;
    public string ProductName;
    { get { return _name; }
    {
      set
      {
          _name = value;
          RasePropertyChanged(() => ProductName);
      }
    }
}