Click or drag to resize

ModelBaseNotifyOfChangeT Method (T, T, String, Action)

Updates a property directly by comparing values and then firing its OnPropertyChanged event if necessary.

Namespace:  Xcalibur.Extensions.MVVM.Models
Assembly:  Xcalibur.Extensions.MVVM (in Xcalibur.Extensions.MVVM.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public void NotifyOfChange<T>(
	T newValue,
	ref T valueReference,
	[CallerMemberNameAttribute] string propertyName = null,
	Action onChanged = null
)

Parameters

newValue
Type: T
The new value.
valueReference
Type: T
The value reference.
propertyName (Optional)
Type: SystemString
Name of the property.
onChanged (Optional)
Type: SystemAction
The on changed.

Type Parameters

T

Implements

INotifyNotifyOfChangeT(T, T, String, Action)
Examples
C#
 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}
See Also