Ejemplo n.º 1
0
        void control_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            var control = e.OriginalSource as Control;

            if (e.Action == ValidationErrorEventAction.Removed && ErrorControls.Contains(control))
            {
                ErrorControls.Remove(control);
            }
            else if (e.Action == ValidationErrorEventAction.Added && !ErrorControls.Contains(control) && control.IsEnabled && control.Visibility == Visibility.Visible)
            {
                ErrorControls.Add(control);
            }
        }
Ejemplo n.º 2
0
        private bool ValidateDateRange(Control p, FrameworkElement element, FieldInfo field)
        {
            DateTime?currentDate = p.GetType().GetProperty("SelectedDate").GetValue(p, null) as DateTime?;
            var      entity      = element.DataContext as ComplexObject;

            string maxDateFrom = RestrictDatePickerAttached.GetMaxDate(element);
            string minDateFrom = RestrictDatePickerAttached.GetMinDate(element);

            if (entity != null && currentDate != null && (currentDate < DateTime.Parse(minDateFrom) || currentDate > DateTime.Parse(maxDateFrom)))
            {
                var be = element.GetBindingExpression((DependencyProperty)field.GetValue(null));
                if (be != null && be.ParentBinding != null && be.ParentBinding.Path != null)
                {
                    if (p.Tag == null)
                    {
                        p.Tag = new ValidationResult(string.Format(ValidationResource.ValidationMessage_InvalidDate, minDateFrom, maxDateFrom), new string[] { be.ParentBinding.Path.Path });
                        entity.ValidationErrors.Add(p.Tag as ValidationResult);
                    }
                    else if (!entity.ValidationErrors.Contains(p.Tag as ValidationResult))
                    {
                        entity.ValidationErrors.Add(p.Tag as ValidationResult);
                    }
                }
                if (!ErrorControls.Contains(p))
                {
                    ErrorControls.Add(element as Control);
                }
                return(false);
            }
            else if (entity != null)
            {
                if (p.Tag != null && entity.ValidationErrors.Contains(p.Tag as ValidationResult))
                {
                    entity.ValidationErrors.Remove(p.Tag as ValidationResult);
                    p.Tag = null;
                }
                if (ErrorControls.Contains(p))
                {
                    ErrorControls.Remove(element as Control);
                }
            }
            return(true);
        }