前回のイベント、デリゲートと似た内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
public partial class Form1 : Form { public Form1() { InitializeComponent(); var p = new Person(); p.PropertyChanged += (s,e) => { MessageBox.Show(e.PropertyName); }; //プロパティが変更されるとイベントが発火。 p.Name = "jiro"; } } class Person : INotifyPropertyChanged { private string _name; public string Name { get { return _name; } set { if (Equals(_name, value)) return; _name = value; OnPropertyChanged(_name); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string name) { if (PropertyChanged == null) return; PropertyChanged(this, new PropertyChangedEventArgs(name)); } } |