mvvm in wpf

observable property

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
if (_firstName == value)
{
return;
}
_firstName = value;
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs("FirstName"));
}
}
}

In order to solve this, MVVM Light proposes a few solutions:

  • The logic needed to raise the PropertyChanged event can be stored in a ViewModelBase class that every ViewModel inherits from.
  • Properties can be identified by a lambda expression instead of a string. This prevents typos or errors when the name of the property changes.
  • The remaining lines can be automated using a code snippet in Visual Studio.

MVVM in WPF

WPF MVVM step by step (Basics to Advance Level)

对应的中文翻译

https://www.oschina.net/translate/wpf-mvvm-step-by-step-basics-to-advance-level?lang=chs&page=2#