Пример #1
0
        // If an already invalid child is set or the previous child was invalid,
        // the aggregated validation state of the owner changes, therefore an
        // ValidationResultChanged event should be raised.
        private void DetectValidationResultChange(IBehaviorContext context, Action action)
        {
            IViewModel oldChild = this.GetValueNext <TValue>(context);

            action();

            IViewModel newChild = this.GetValueNext <TValue>(context);

            // It is important to query the 'oldResult' after executing the 'action'. The
            // following scenario could happen otherwise:
            //   1. 'oldResult' is assigned before the action and is valid.
            //   2. The action is executed and makes 'oldChild' invalid and 'newChild'
            //      stays valid. Since 'oldChild' is still in the hierarchy at this
            //      time, the error is propagated to the validation caches of the
            //      parent.
            //   3. 'newResult' is assigned to result of the 'newChild' (valid).
            //   4. We would not detect a change in the 'if' below and therefore not
            //      raise a change which would leave the validation cache of the parents
            //      with the stale error.
            ValidationResult oldResult = GetValidationResultOrValidIfNull(oldChild);
            ValidationResult newResult = GetValidationResultOrValidIfNull(newChild);

            if (!Object.Equals(oldResult, newResult))
            {
                context.NotifyChange(ChangeArgs.ValidationResultChanged());
            }
        }
Пример #2
0
        public void HandleChange(IBehaviorContext context, CollectionChangedArgs <TItemVM> args)
        {
            IEnumerable <TItemVM> changedItems = args
                                                 .OldItems
                                                 .Concat(args.NewItems);

            IEnumerable <ValidationResult> changedItemsResults = changedItems
                                                                 .Select(x => x.Kernel.GetValidationResult());

            var changedItemsResult = ValidationResult.Join(changedItemsResults);

            this.HandleChangeNext(context, args);

            // If already invalid items are added or removed, the aggregated
            // validation state of the collection owner changes, therefore an
            // ValidationResultChanged event should be raised.
            if (!changedItemsResult.IsValid)
            {
                context.NotifyChange(ChangeArgs.ValidationResultChanged(args.Reason));
            }
        }
Пример #3
0
        public override void InitializeValue(IBehaviorContext context)
        {
            base.InitializeValue(context);

            TValue childVM = this.GetValueNext <TValue>(context);

            if (childVM != null)
            {
                ValidationResult childValidatonResult = childVM
                                                        .Kernel
                                                        .GetValidationResult();

                if (!childValidatonResult.IsValid)
                {
                    // If 'PopulatedWith' returns an already invalid item, the aggregated
                    // validation state of the collection owner changes, therefore an
                    // 'ValidationResultChanged' event should be raised.
                    if (!childValidatonResult.IsValid)
                    {
                        context.NotifyChange(ChangeArgs.ValidationResultChanged());
                    }
                }
            }
        }