/// <summary>
        /// This method is invoked when the Child property changes.
        /// http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/ContentControl.cs,262
        /// </summary>
        /// <param name="oldChild">The old value of the Child property.</param>
        /// <param name="newChild">The new value of the Child property.</param>
        protected virtual void OnChildChanged(BaseBox oldChild, BaseBox newChild)
        {
            this.RemoveLogicalChild(oldChild);

            if (newChild != null)
            {
                DependencyObject logicalParent = LogicalTreeHelper.GetParent(newChild);
                if (logicalParent != null)
                {
                    if (this.TemplatedParent != null && FrameworkObject.IsEffectiveAncestor(logicalParent, this))
                    {
                        // In the case that this SpinnerDecorator belongs in a parent template
                        // and represents the content of a parent, we do not wish to change
                        // the logical ancestry of the content.
                        return;
                    }
                    else
                    {
                        // If the new content was previously hooked up to the logical
                        // tree then we sever it from the old parent.
                        var message = "Cannot add child that already belongs to a parent.\r\n" +
                                      "Fixing this requires more source diving than I feel like right now.\r\n" +
                                      "Waiting to see if it becomes a problem";
                        throw new NotSupportedException(message);
                        //LogicalTreeHelper.RemoveLogicalChild(logicalParent, newChild);
                    }
                }
            }

            // Add the new content child
            this.AddLogicalChild(newChild);
        }
        public static RegexValidationResult CreateErrorResult(string text, BaseBox box)
        {
            var formatAndCulture = PleaseProvideValidInputFormatAndCulture.GetOrCreate(box.Culture);
            var message          = formatAndCulture.Format;

            return(new RegexValidationResult(text, box.RegexPattern, null, box.Culture, formatAndCulture, false, message));
        }
        public static RegexValidationResult CreateErrorResult(string text, BaseBox box)
        {
            var formatAndCulture = PleaseProvideValidInputFormatAndCulture.GetOrCreate(box.Culture);
            var message          = formatAndCulture.Format;

            return(new RegexValidationResult(
                       text: text,
                       pattern: box.RegexPattern,
                       exception: null,
                       currentBoxCulture: box.Culture,
                       formatAndCulture: formatAndCulture,
                       isValid: false,
                       errorContent: message));
        }
        public static RegexValidationResult CreateMalformedPatternErrorResult(string text, Exception exception, BaseBox box)
        {
            if (box is null)
            {
                throw new ArgumentNullException(nameof(box));
            }

            var formatAndCulture = SyntaxErrorInRegexPatternFormatAndCulture.GetOrCreate(box.Culture);
            var message          = formatAndCulture.Format;

            return(new RegexValidationResult(
                       text: text,
                       pattern: box.RegexPattern,
                       exception: exception,
                       currentBoxCulture: box.Culture,
                       formatAndCulture: formatAndCulture,
                       isValid: false,
                       errorContent: message));
        }
 public FormattedView(BaseBox baseBox)
 {
     this.baseBox = baseBox;
 }
 public FormattedView(BaseBox baseBox)
 {
     this.baseBox = baseBox;
 }
        internal static object ValidateAndGetValue(BaseBox box)
        {
            var converter  = box.TextValueConverter;
            var expression = box.TextBindingExpression;

            if (converter is null)
            {
                Validation.MarkInvalid(expression, new ValidationError(RegexValidationRule.FromText, expression.ParentBinding, $"{BaseBox.TextValueConverterProperty.Name} == null", null));
                return(Binding.DoNothing);
            }

            var validated = false;
            var value     = Default;

            foreach (var rule in box.ValidationRules)
            {
                switch (rule.ValidationStep)
                {
                case ValidationStep.RawProposedValue:
                {
                    var result = rule.Validate(box.Text, GetCulture(expression), expression);
                    if (!result.IsValid)
                    {
                        Validation.MarkInvalid(expression, new ValidationError(rule, expression.ParentBinding, result, null));
                        return(Binding.DoNothing);
                    }

                    validated = true;
                    break;
                }

                case ValidationStep.UpdatedValue:
                case ValidationStep.CommittedValue:
                case ValidationStep.ConvertedProposedValue:
                {
                    if (value == Default)
                    {
                        value = converter.Convert(box.Text, null, box, GetCulture(expression));
                    }

                    if (value == Binding.DoNothing)
                    {
                        continue;
                    }

                    var result = rule.Validate(value, GetCulture(expression), expression);
                    if (!result.IsValid)
                    {
                        Validation.MarkInvalid(expression, new ValidationError(rule, expression.ParentBinding, result, null));
                        return(Binding.DoNothing);
                    }

                    validated = true;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException(nameof(box), rule.ValidationStep, "Unhandled validation step.");
                }
            }

            if (validated)
            {
                Validation.ClearInvalid(expression);
            }

            if (value == Default)
            {
                value = converter.Convert(box.Text, null, box, GetCulture(expression));
            }

            return(value);
        }
 internal static void UpdateValidation(BaseBox box)
 {
     ValidateAndGetValue(box);
 }
 internal FormattedView(BaseBox baseBox)
 {
     this.baseBox = baseBox;
 }
        public static RegexValidationResult CreateMalformedPatternErrorResult(string text, Exception exception, BaseBox box)
        {
            var formatAndCulture = SyntaxErrorInRegexPatternFormatAndCulture.GetOrCreate(box.Culture);
            var message          = formatAndCulture.Format;

            return(new RegexValidationResult(text, box.RegexPattern, exception, box.Culture, formatAndCulture, false, message));
        }
        internal static object ValidateAndGetValue(BaseBox box)
        {
            var converter = box.TextValueConverter;
            var expression = box.TextBindingExpression;

            if (converter == null)
            {
                Validation.MarkInvalid(expression, new ValidationError(IsMatch.FromText, expression.ParentBinding, $"{BaseBox.TextValueConverterProperty.Name} == null", null));
                return Binding.DoNothing;
            }

            bool validated = false;
            var value = Default;
            foreach (var rule in box.ValidationRules)
            {
                switch (rule.ValidationStep)
                {
                    case ValidationStep.RawProposedValue:
                        {
                            var result = rule.Validate(box.Text, GetCulture(expression), expression);
                            if (!result.IsValid)
                            {
                                Validation.MarkInvalid(expression, new ValidationError(rule, expression.ParentBinding, result, null));
                                return Binding.DoNothing;
                            }

                            validated = true;
                            break;
                        }

                    case ValidationStep.UpdatedValue:
                    case ValidationStep.CommittedValue:
                    case ValidationStep.ConvertedProposedValue:
                        {
                            if (value == Default)
                            {
                                value = converter.Convert(box.Text, null, box, GetCulture(expression));
                            }

                            if (value == Binding.DoNothing)
                            {
                                continue;
                            }

                            var result = rule.Validate(value, GetCulture(expression), expression);
                            if (!result.IsValid)
                            {
                                Validation.MarkInvalid(expression, new ValidationError(rule, expression.ParentBinding, result, null));
                                return Binding.DoNothing;
                            }

                            validated = true;
                            break;
                        }

                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            if (validated)
            {
                Validation.ClearInvalid(expression);
            }

            if (value == Default)
            {
                value = converter.Convert(box.Text, null, box, GetCulture(expression));
            }

            return value;
        }
 internal static void UpdateValidation(BaseBox box)
 {
     ValidateAndGetValue(box);
 }