Ejemplo n.º 1
0
        /// <summary>
        /// Properties the name changed.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="arg">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void PropertyNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs arg)
        {
            var control            = (Control)dependencyObject;
            var collection         = new BehaviorCollection();
            var validationBehavior = new ValidationBehavior()
            {
                PropertyName = arg.NewValue.ToString()
            };
            var changePropertyIsValidAction = new ChangePropertyAction
            {
                PropertyName = new PropertyPath(nameof(Control.Style)),
                Value        = null
            };

            var errorStyle = new Style
            {
                TargetType = typeof(Control)
            };

            errorStyle.Setters.Add(new Setter(Control.BorderBrushProperty, new SolidColorBrush(Colors.Red)));

            var changePropertyIsInvalidAction = new ChangePropertyAction
            {
                PropertyName = new PropertyPath(nameof(Control.Style)),
                Value        = errorStyle
            };

            validationBehavior.WhenValidActions.Add(changePropertyIsValidAction);
            validationBehavior.WhenInvalidActions.Add(changePropertyIsInvalidAction);

            collection.Add(validationBehavior);
            control.SetValue(Interaction.BehaviorsProperty, collection);
        }
        /// <summary>
        /// Properties the name changed.
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="arg">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void PropertyNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs arg)
        {
            var control = (Control)dependencyObject;

            _validationBehavior = new ValidationBehavior()
            {
                PropertyName = arg.NewValue.ToString()
            };
            control.Loaded += Control_Loaded;
        }