|
9 | 9 |
|
10 | 10 | using System; |
11 | 11 | using System.Collections.Generic; |
12 | | -using System.Linq.Expressions; |
13 | 12 | using System.Runtime.CompilerServices; |
14 | 13 | using Microsoft.Toolkit.Mvvm.Messaging; |
15 | 14 | using Microsoft.Toolkit.Mvvm.Messaging.Messages; |
@@ -204,7 +203,14 @@ protected bool SetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comp |
204 | 203 | /// </remarks> |
205 | 204 | protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, bool broadcast, [CallerMemberName] string? propertyName = null) |
206 | 205 | { |
207 | | - return SetProperty(oldValue, newValue, EqualityComparer<T>.Default, callback, broadcast, propertyName); |
| 206 | + bool propertyChanged = SetProperty(oldValue, newValue, callback, propertyName); |
| 207 | + |
| 208 | + if (propertyChanged && broadcast) |
| 209 | + { |
| 210 | + Broadcast(oldValue, newValue, propertyName); |
| 211 | + } |
| 212 | + |
| 213 | + return propertyChanged; |
208 | 214 | } |
209 | 215 |
|
210 | 216 | /// <summary> |
@@ -237,40 +243,53 @@ protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> compa |
237 | 243 | /// Compares the current and new values for a given nested property. If the value has changed, |
238 | 244 | /// raises the <see cref="ObservableObject.PropertyChanging"/> event, updates the property and then raises the |
239 | 245 | /// <see cref="ObservableObject.PropertyChanged"/> event. The behavior mirrors that of |
240 | | - /// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,string)"/>, with the difference being that this |
| 246 | + /// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string)"/>, with the difference being that this |
241 | 247 | /// method is used to relay properties from a wrapped model in the current instance. For more info, see the docs for |
242 | | - /// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,string)"/>. |
| 248 | + /// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string)"/>. |
243 | 249 | /// </summary> |
244 | | - /// <typeparam name="T">The type of property to set.</typeparam> |
245 | | - /// <param name="propertyExpression">An <see cref="Expression{TDelegate}"/> returning the property to update.</param> |
| 250 | + /// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam> |
| 251 | + /// <typeparam name="T">The type of property (or field) to set.</typeparam> |
| 252 | + /// <param name="oldValue">The current property value.</param> |
246 | 253 | /// <param name="newValue">The property's value after the change occurred.</param> |
| 254 | + /// <param name="model">The model </param> |
| 255 | + /// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param> |
247 | 256 | /// <param name="broadcast">If <see langword="true"/>, <see cref="Broadcast{T}"/> will also be invoked.</param> |
248 | 257 | /// <param name="propertyName">(optional) The name of the property that changed.</param> |
249 | 258 | /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns> |
250 | | - protected bool SetProperty<T>(Expression<Func<T>> propertyExpression, T newValue, bool broadcast, [CallerMemberName] string? propertyName = null) |
| 259 | + protected bool SetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, bool broadcast, [CallerMemberName] string? propertyName = null) |
251 | 260 | { |
252 | | - return SetProperty(propertyExpression, newValue, EqualityComparer<T>.Default, broadcast, propertyName); |
| 261 | + bool propertyChanged = SetProperty(oldValue, newValue, model, callback, propertyName); |
| 262 | + |
| 263 | + if (propertyChanged && broadcast) |
| 264 | + { |
| 265 | + Broadcast(oldValue, newValue, propertyName); |
| 266 | + } |
| 267 | + |
| 268 | + return propertyChanged; |
253 | 269 | } |
254 | 270 |
|
255 | 271 | /// <summary> |
256 | 272 | /// Compares the current and new values for a given nested property. If the value has changed, |
257 | 273 | /// raises the <see cref="ObservableObject.PropertyChanging"/> event, updates the property and then raises the |
258 | 274 | /// <see cref="ObservableObject.PropertyChanged"/> event. The behavior mirrors that of |
259 | | - /// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,IEqualityComparer{T},string)"/>, |
| 275 | + /// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,IEqualityComparer{T},TModel,Action{TModel,T},string)"/>, |
260 | 276 | /// with the difference being that this method is used to relay properties from a wrapped model in the |
261 | 277 | /// current instance. For more info, see the docs for |
262 | | - /// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,IEqualityComparer{T},string)"/>. |
| 278 | + /// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,IEqualityComparer{T},TModel,Action{TModel,T},string)"/>. |
263 | 279 | /// </summary> |
264 | | - /// <typeparam name="T">The type of property to set.</typeparam> |
265 | | - /// <param name="propertyExpression">An <see cref="Expression{TDelegate}"/> returning the property to update.</param> |
| 280 | + /// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam> |
| 281 | + /// <typeparam name="T">The type of property (or field) to set.</typeparam> |
| 282 | + /// <param name="oldValue">The current property value.</param> |
266 | 283 | /// <param name="newValue">The property's value after the change occurred.</param> |
267 | 284 | /// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use to compare the input values.</param> |
| 285 | + /// <param name="model">The model </param> |
| 286 | + /// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param> |
268 | 287 | /// <param name="broadcast">If <see langword="true"/>, <see cref="Broadcast{T}"/> will also be invoked.</param> |
269 | 288 | /// <param name="propertyName">(optional) The name of the property that changed.</param> |
270 | 289 | /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns> |
271 | | - protected bool SetProperty<T>(Expression<Func<T>> propertyExpression, T newValue, IEqualityComparer<T> comparer, bool broadcast, [CallerMemberName] string? propertyName = null) |
| 290 | + protected bool SetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, bool broadcast, [CallerMemberName] string? propertyName = null) |
272 | 291 | { |
273 | | - bool propertyChanged = SetProperty(propertyExpression, newValue, comparer, out T oldValue, propertyName); |
| 292 | + bool propertyChanged = SetProperty(oldValue, newValue, comparer, model, callback, propertyName); |
274 | 293 |
|
275 | 294 | if (propertyChanged && broadcast) |
276 | 295 | { |
|
0 commit comments