/
ObservableObject.cs
174 lines (155 loc) · 8.98 KB
/
ObservableObject.cs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//-----------------------------------------------------------------------
// <copyright file="ObservableObject.cs" company="MyToolkit">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/MyToolkit/MyToolkit/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using MyToolkit.Model;
using MyToolkit.Utilities;
namespace MyToolkit.Model
{
/// <summary>Provides an implementation of the <see cref="INotifyPropertyChanged"/> interface. </summary>
[DataContract]
public class ObservableObject : INotifyPropertyChanged
{
/// <summary>Occurs when a property value changes. </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="propertyName">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
public bool Set<T>(ref T oldValue, T newValue, [CallerMemberName] String propertyName = null)
{
return Set(propertyName, ref oldValue, newValue);
}
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <typeparam name="T">The type of the property. </typeparam>
/// <param name="propertyNameExpression">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
public bool Set<T>(Expression<Func<T>> propertyNameExpression, ref T oldValue, T newValue)
{
return Set(ExpressionUtilities.GetPropertyName(propertyNameExpression), ref oldValue, newValue);
}
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <typeparam name="TClass">The type of the class with the property. </typeparam>
/// <typeparam name="TProp">The type of the property. </typeparam>
/// <param name="propertyNameExpression">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
public bool Set<TClass, TProp>(Expression<Func<TClass, TProp>> propertyNameExpression, ref TProp oldValue,
TProp newValue)
{
return Set(ExpressionUtilities.GetPropertyName(propertyNameExpression), ref oldValue, newValue);
}
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="propertyName">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
public virtual bool Set<T>(String propertyName, ref T oldValue, T newValue)
{
if (Equals(oldValue, newValue))
return false;
oldValue = newValue;
RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
return true;
}
/// <summary>Raises the property changed event. </summary>
/// <param name="propertyName">The property name. </param>
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>Raises the property changed event. </summary>
/// <param name="propertyNameExpression">The property name as lambda. </param>
public void RaisePropertyChanged(Expression<Func<object>> propertyNameExpression)
{
RaisePropertyChanged(new PropertyChangedEventArgs(ExpressionUtilities.GetPropertyName(propertyNameExpression)));
}
/// <summary>Raises the property changed event. </summary>
/// <typeparam name="TClass">The type of the class with the property. </typeparam>
/// <param name="propertyNameExpression">The property name as lambda. </param>
public void RaisePropertyChanged<TClass>(Expression<Func<TClass, object>> propertyNameExpression)
{
RaisePropertyChanged(new PropertyChangedEventArgs(ExpressionUtilities.GetPropertyName(propertyNameExpression)));
}
/// <summary>Raises the property changed event. </summary>
/// <param name="args">The arguments. </param>
protected virtual void RaisePropertyChanged(PropertyChangedEventArgs args)
{
var copy = PropertyChanged;
if (copy != null)
copy(this, args);
}
/// <summary>Raises the property changed event for all properties (string.Empty). </summary>
public void RaiseAllPropertiesChanged()
{
RaisePropertyChanged(new PropertyChangedEventArgs(string.Empty));
}
#region Obsolete
/// <summary>If the new value does not equal the old value, then updates the property and raises the changed event. </summary>
/// <typeparam name="T">The type of the property. </typeparam>
/// <param name="propertyNameExpression">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
[Obsolete("Use Set method without lambda instead. 5/17/2014")]
public bool SetProperty<T>(Expression<Func<T>> propertyNameExpression, ref T oldValue, T newValue)
{
return Set(ExpressionUtilities.GetPropertyName(propertyNameExpression), ref oldValue, newValue);
}
/// <summary>If the new value does not equal the old value, then updates the property and raises the changed event. </summary>
/// <typeparam name="TClass">The type of the class with the property. </typeparam>
/// <typeparam name="TProp">The type of the property. </typeparam>
/// <param name="propertyNameExpression">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
[Obsolete("Use Set method instead. 5/17/2014")]
public bool SetProperty<TClass, TProp>(Expression<Func<TClass, TProp>> propertyNameExpression,
ref TProp oldValue, TProp newValue)
{
return Set(ExpressionUtilities.GetPropertyName(propertyNameExpression), ref oldValue, newValue);
}
/// <summary>If the new value does not equal the old value, then updates the property and raises the changed event. </summary>
/// <param name="propertyName">The property name. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
[Obsolete("Use Set method instead. 5/17/2014")]
public bool SetProperty<T>(String propertyName, ref T oldValue, T newValue)
{
return Set(propertyName, ref oldValue, newValue);
}
/// <summary>If the new value does not equal the old value, then updates the property and raises the changed event. </summary>
/// <param name="propertyName">The property name. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
[Obsolete("Use Set method instead. 5/17/2014")]
public bool SetProperty<T>(ref T oldValue, T newValue, [CallerMemberName] String propertyName = null)
{
return Set(propertyName, ref oldValue, newValue);
}
#endregion
}
}
namespace MyToolkit.MVVM
{
/// <summary>Provides an implementation of the <see cref="INotifyPropertyChanged"/> interface. </summary>
[Obsolete("Use MyToolkit.Mvvm.ObservableObject instead. 5/17/2014")]
[DataContract]
public class NotifyPropertyChanged : ObservableObject
{
}
}