/// <summary> /// Sets the <paramref name="field"/> and calls property changed for it and any others given with a <see cref="AlsoNotifyAttribute"/>. /// </summary> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="field">Field to set.</param> /// <param name="newValue">New value of the field.</param> /// <param name="trackChanges">True if <see cref="BeginEdit"/> should be called if the value changes.</param> /// <param name="propertyName">Name of the property to notify with.</param> /// <returns>Returns true if new value was set</returns> protected bool SetProperty <TValue>(ref TValue field, TValue newValue, bool trackChanges = true, [CallerMemberName] string propertyName = null) { if (EqualityComparer <TValue> .Default.Equals(field, newValue)) { return(false); } if (trackChanges) { BeginEdit(); } field = newValue; RaisePropertyChanged(propertyName); if (AlsoNotifyMap.TryGetValue(propertyName, out var alsoNotify)) { foreach (var propName in alsoNotify) { RaisePropertyChanged(propName); } } return(true); }
private void SetupAlsoNotify() { var alsoNotifyProperties = Accessor.GetMembers().Where(m => m.IsDefined(typeof(AlsoNotifyAttribute))); foreach (var propertyInfo in alsoNotifyProperties) { var attr = (AlsoNotifyAttribute)propertyInfo.GetAttribute(typeof(AlsoNotifyAttribute), true); AlsoNotifyMap.Add(propertyInfo.Name, attr.AlsoNotify); } }