Example #1
0
        /// <summary>
        /// Called when the value of a validating property changes.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">Event arguments.</param>
        private static void DependencyProperty_ValueChanged(object sender, EventArgs e)
        {
            DependencyObject   dependencyObject = (DependencyObject)sender;
            DependencyProperty?property         = ValidatingPropertyBehavior.GetValidatingProperty(dependencyObject);

            if (property != null)
            {
                dependencyObject.Validate(property);
            }
        }
Example #2
0
        /// <summary>
        /// Validate that a value of a dependency object/property combination does not violate any binding validation rules.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="dependencyProperty">The dependency property.</param>
        private static void Validate(this DependencyObject dependencyObject, DependencyProperty dependencyProperty)
        {
            BindingExpressionBase bindingExpression = BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty);

            if (bindingExpression == null)
            {
                return;
            }
            switch (bindingExpression.ParentBindingBase)
            {
            case Binding binding:
                ValidatingPropertyBehavior.Validate(dependencyObject.GetValue(dependencyProperty), binding.ValidationRules, bindingExpression);
                break;

            case MultiBinding multiBinding:
                ValidatingPropertyBehavior.Validate(dependencyObject.GetValue(dependencyProperty), multiBinding.ValidationRules, bindingExpression);
                break;
            }
        }