/// <summary> /// Updates multiple profile properties. /// </summary> /// <typeparam name="TProperty">The type of the value to update.</typeparam> /// <typeparam name="TModel">The type of the model to update it on.</typeparam> /// <param name="targetFunc">Func to get the target model.</param> /// <param name="updateAction">An action to perform the update on the model.</param> /// <param name="raisePropertyName">The name to use when raising the PropertyChanged event.</param> /// <param name="value">The new value.</param> protected void UpdateProfileProperties <TProperty, TModel>(Func <TModel> targetFunc, Action <TModel, TProperty> updateAction, string raisePropertyName, TProperty value) { if (!this.profileProperties.ContainsKey(raisePropertyName)) { throw new ArgumentException("UpdatePresetProperty called on " + raisePropertyName + " without registering."); } bool presetModified = false; if (!this.AutomaticChange) { if (!this.Preset.IsModified) { // Clone the profile so we modify a different copy. VCProfile newProfile = new VCProfile(); newProfile.InjectFrom <CloneInjection>(this.Profile); if (!this.Preset.IsModified) { this.presetsService.PrepareModifyPreset(newProfile); presetModified = true; } } } // Update the value and raise PropertyChanged updateAction(targetFunc(), value); this.RaisePropertyChanged(raisePropertyName); if (presetModified) { this.presetsService.FinalizeModifyPreset(); } if (!this.AutomaticChange) { // If we have an action registered to update dependent properties, do it Action <object> action = this.profileProperties[raisePropertyName]; if (action != null) { // Protect against update loops with a flag this.AutomaticChange = true; action(null); this.AutomaticChange = false; } this.presetsService.SaveUserPresets(); } }
/// <summary> /// Updates the profile property (power user version). /// </summary> /// <typeparam name="TProperty">The type of the value to update.</typeparam> /// <typeparam name="TModel">The type of the model to update it on.</typeparam> /// <param name="targetFunc">Func to get the target model.</param> /// <param name="propertyName">The name of the property on the model to update.</param> /// <param name="raisePropertyName">The name to use when raising the PropertyChanged event.</param> /// <param name="value">The new value.</param> /// <param name="raisePropertyChanged">True to raise the PropertyChanged event.</param> protected void UpdateProfileProperty <TProperty, TModel>(Func <TModel> targetFunc, string propertyName, string raisePropertyName, TProperty value, bool raisePropertyChanged = true) { TModel originalTarget = targetFunc(); if (!this.profileProperties.ContainsKey(raisePropertyName)) { throw new ArgumentException("UpdatePresetProperty called on " + raisePropertyName + " without registering."); } Type type = typeof(TModel); PropertyInfo property = type.GetProperty(propertyName); var oldValue = (TProperty)property.GetValue(originalTarget); if (oldValue != null && oldValue.Equals(value)) { return; } bool presetModified = false; if (!this.AutomaticChange) { if (!this.Preset.IsModified) { // Clone the profile so we modify a different copy. VCProfile newProfile = new VCProfile(); newProfile.InjectFrom <CloneInjection>(this.Profile); if (!this.Preset.IsModified) { this.presetsService.PrepareModifyPreset(newProfile); presetModified = true; } } } // Update the value and raise PropertyChanged property.SetValue(targetFunc(), value); if (raisePropertyChanged) { this.RaisePropertyChanged(raisePropertyName); } if (presetModified) { this.presetsService.FinalizeModifyPreset(); } if (!this.AutomaticChange) { // If we have an action registered to update dependent properties, do it Action <object> action = this.profileProperties[raisePropertyName]; if (action != null) { // Protect against update loops with a flag this.AutomaticChange = true; action(oldValue); this.AutomaticChange = false; } this.presetsService.SaveUserPresets(); } }