-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPropertyChangeNotifier.cs
More file actions
92 lines (78 loc) · 2.76 KB
/
PropertyChangeNotifier.cs
File metadata and controls
92 lines (78 loc) · 2.76 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
#endregion
/// <summary>
/// Provides a simple implementation of the <see cref="INotifyPropertyChanged"/> interface for data binding.
/// </summary>
/// <remarks>
/// Derived classes can easily add new properties using the inherited <see cref="Update"/> method.
/// For example, you can add a Comment property like:
/// <code>
/// private string comment;
/// public string Comment { get => this.comment; set => this.Update(ref this.comment, value); }
/// </code>
/// </remarks>
public abstract class PropertyChangeNotifier : INotifyPropertyChanged
{
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
protected PropertyChangeNotifier()
{
}
#endregion
#region Public Events
/// <summary>
/// Raised when a property's value has changed.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
#endregion
#region Protected Methods
/// <summary>
/// Used to update a member variable and raise the <see cref="PropertyChanged"/> event if necessary.
/// </summary>
/// <typeparam name="T">The data type of the member being updated.</typeparam>
/// <param name="member">A reference to the member being updated.</param>
/// <param name="value">The new value to store in the member variable.</param>
/// <param name="callerMemberName">The name of the caller. Normally, you shouldn't pass anything
/// for this parameter. If omitted, the C# compiler will inject the caller's property name automatically
/// using the <see cref="CallerMemberNameAttribute"/>.</param>
/// <returns>True if the member was updated. False if the member wasn't updated.</returns>
protected virtual bool Update<T>(ref T member, T value, [CallerMemberName] string? callerMemberName = null)
{
bool result = false;
if (!EqualityComparer<T>.Default.Equals(member, value))
{
member = value;
result = true;
this.OnPropertyChanged(callerMemberName ?? string.Empty);
}
return result;
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
/// <remarks>
/// This method is useful when setting one property via <see cref="Update"/>
/// also causes a dependent "read-only" property to be updated.
/// </remarks>
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler? handler = this.PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs args = new(propertyName);
handler(this, args);
}
}
#endregion
}
}