/// <summary> /// Called to end an edit operation /// </summary> /// <param name="vm"></param> /// <param name="success">True for persist, False to cancel edits</param> public void OnEndEdit(ValidatingViewModel vm, bool success) { if (!success) { RestoreFieldValues(vm, _savedState); } _savedState = null; }
/// <summary> /// Returns the fields to capture /// </summary> /// <param name="target"></param> /// <returns></returns> private IEnumerable <FieldInfo> GetFieldsToSerialize(ValidatingViewModel target) { EditingViewModel evm = target as EditingViewModel; Predicate <FieldInfo> testField = (evm != null && evm.FieldPredicate != null) ? evm.FieldPredicate : f => true; return(target.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(f => f.GetType() != typeof(ShallowCopyEditableObject) && testField(f))); }
public void ReturnsTrueForOutdatedValidationContext() { var vm = new ValidatingViewModel(); vm.FirstName = "some value"; var lastUpdated = vm.ValidationContext.LastModifiedTicks; vm.FirstName = null; Assert.IsTrue(vm.IsValidationSummaryOutdated(lastUpdated, true)); }
private List <string> CaptureErrorChanges(ValidatingViewModel obj, Action action) { var changes = new List <string>(); obj.ErrorsChanged += (sender, e) => { changes.Add(e.PropertyName); }; action(); return(changes); }
public void ReturnsFalseForNotOutdatedValidationContext() { var vm = new ValidatingViewModel(); vm.FirstName = "some value"; var lastUpdated = DateTime.Now.Ticks + 1; // Only full .NET supports reliable stopwatch, all other frameworks always assume outdated #if NET Assert.IsFalse(vm.IsValidationSummaryOutdated(lastUpdated, true)); #else Assert.IsTrue(vm.IsValidationSummaryOutdated(lastUpdated, true)); #endif }
/// <summary> /// This restores the state of the current object from the passed clone object. /// </summary> /// <param name="target"></param> /// <param name="fieldValues">Object to restore state from</param> private void RestoreFieldValues(ValidatingViewModel target, Dictionary <string, object> fieldValues) { foreach (FieldInfo fi in GetFieldsToSerialize(target)) { object value; if (fieldValues.TryGetValue(fi.Name, out value)) { fi.SetValue(target, value); } else { Debug.WriteLine("Failed to restore field " + fi.Name + " from cloned values, field not found in Dictionary."); } } }
private void DialogHost_Closing(object sender, CancelEventArgs e) { string errorIfInvalidIdentifier = this.Model.GetErrorIfInvalidIdentifier(this.AreaName); if (!string.IsNullOrEmpty(errorIfInvalidIdentifier)) { ValidatingViewModel.DisplayErrorMessage(this.DialogHost, errorIfInvalidIdentifier); e.Cancel = true; return; } if (this.Model.AreaExists(this.AreaName)) { ValidatingViewModel.DisplayErrorMessage(this.DialogHost, Resources.AreaExistsMessage); e.Cancel = true; } }
/// <summary> /// This is used to clone the object. Override the method to provide a more efficient clone. The /// default implementation simply reflects across the object copying every field. /// </summary> /// <returns>Clone of current object</returns> private Dictionary <string, object> GetFieldValues(ValidatingViewModel target) { return(GetFieldsToSerialize(target) .Select(fi => new { Key = fi.Name, Value = fi.GetValue(target) }) .ToDictionary(k => k.Key, k => k.Value)); }
/// <summary> /// Called to begin an edit operation /// </summary> public void OnBeginEdit(ValidatingViewModel vm) { _savedState = GetFieldValues(vm); }