处理 PropertyChanged 事件
片段
public class BugReport : INotifyPropertyChanged
{
public string Title { ... }
public BugStatus Status { ... }
}
...
private void BugReport_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var bugReport = (BugReport)sender;
switch (e.PropertyName)
{
case nameof(bugReport.Title):
Console.WriteLine("{0} changed to {1}", e.PropertyName, bugReport.Title);
break;
case nameof(bugReport.Status):
Console.WriteLine("{0} changed to {1}", e.PropertyName, bugReport.Status);
break;
}
}
...
var report = new BugReport();
report.PropertyChanged += BugReport_PropertyChanged;
report.Title = "Everything is on fire and broken";
report.Status = BugStatus.ShowStopper;
控制台输出
标题改为一切都着火了
状态已更改为 ShowStopper