ModelBaseNotifyOfChangeT Method (ExpressionFuncT, T, T, Action) |
Namespace: Xcalibur.Extensions.MVVM.Models
public void NotifyOfChange<T>( Expression<Func<T>> property, T newValue, ref T valueReference, Action onChanged = null )
1/// Before as per How to: Implement Property Change Notification 2/// https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification 3 4// This class implements INotifyPropertyChanged 5// to support one-way and two-way bindings 6// (such that the UI element updates when the source 7// has been changed dynamically) 8public class Person : INotifyPropertyChanged 9{ 10 private string name; 11 // Declare the event 12 public event PropertyChangedEventHandler PropertyChanged; 13 14 public Person() 15 { 16 } 17 18 public Person(string value) 19 { 20 this.name = value; 21 } 22 23 public string PersonName 24 { 25 get { return name; } 26 set 27 { 28 name = value; 29 // Call OnPropertyChanged whenever the property is updated 30 OnPropertyChanged("PersonName"); 31 } 32 } 33 34 // Create the OnPropertyChanged method to raise the event 35 protected void OnPropertyChanged(string name) 36 { 37 PropertyChangedEventHandler handler = PropertyChanged; 38 if (handler != null) 39 { 40 handler(this, new PropertyChangedEventArgs(name)); 41 } 42 } 43} 44 45/// After 46/// Implementing responsive properties should not be so difficult. 47public class Person : ModelBase 48{ 49 private string name; 50 51 public Person() 52 { 53 } 54 55 public Person(string value) 56 { 57 this.name = value; 58 } 59 60 public string PersonName 61 { 62 get => name; 63 set => NotifyOfChange(value, ref name); 64 } 65}