private void ExecuteClick(object sender)
        {
            ListBox lb = sender as ListBox;

            if (lb != null)
            {
                ValidationSummaryItem vsi = lb.SelectedItem as ValidationSummaryItem;
                if (vsi != null && this.FocusControlsOnClick)
                {
                    int idx;
                    // Ensure the currently selected item source is valid
                    if (vsi.Sources.Count == 0)
                    {
                        // Clear the current ESI source if the ESI has none, such as when the ESI has changed
                        this._currentValidationSummaryItemSource = null;
                    }
                    else
                    {
                        // If the current ESI source is not part of the current set, select the first one by default.
                        idx = FindMatchingErrorSource(vsi.Sources, this._currentValidationSummaryItemSource);
                        if (idx < 0)
                        {
                            this._currentValidationSummaryItemSource = vsi.Sources[0];
                        }
                    }

                    // Raise the event
                    FocusingInvalidControlEventArgs e = new FocusingInvalidControlEventArgs(vsi, this._currentValidationSummaryItemSource);
                    this.OnFocusingInvalidControl(e);

                    // Raise the AutomationPeer event
                    ValidationSummaryAutomationPeer peer = ValidationSummaryAutomationPeer.FromElement(this) as ValidationSummaryAutomationPeer;
                    if (peer != null && AutomationPeer.ListenerExists(AutomationEvents.InvokePatternOnInvoked))
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
                    }

                    // Focus the target, which will usually be the current ESI source or the overwritten one
                    if (!e.Handled && e.Target != null && e.Target.Control != null)
                    {
                        e.Target.Control.Focus();
                    }

                    // Update currently selected item, but only if there are multiple ESI sources.
                    if (vsi.Sources.Count > 0)
                    {
                        idx = FindMatchingErrorSource(vsi.Sources, e.Target);
                        idx = idx < 0 ? 0 : ++idx % vsi.Sources.Count;
                        this._currentValidationSummaryItemSource = vsi.Sources[idx];
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the ValidationSummaryItem class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="messageHeader">The header/prefix of the item, such as the property name.</param>
 /// <param name="itemType">The type of error, such as Property or Entity level.</param>
 /// <param name="source">The source of the error message, including the originating control and/or property name.</param>
 /// <param name="context">Context from which the error occurred.  This general property can be used as a container to keep track of the entity, for instance.</param>
 public ValidationSummaryItem(string message, string messageHeader, ValidationSummaryItemType itemType, ValidationSummaryItemSource source, object context)
 {
     this.MessageHeader = messageHeader;
     this.Message = message;
     this.ItemType = itemType;
     this.Context = context;
     this.Sources = new ObservableCollection<ValidationSummaryItemSource>();
     if (source != null)
     {
         this.Sources.Add(source);
     }
 }
 /// <summary>
 /// Initializes a new instance of the ValidationSummaryItem class.
 /// </summary>
 /// <param name="message">The error message.</param>
 /// <param name="messageHeader">The header/prefix of the item, such as the property name.</param>
 /// <param name="itemType">The type of error, such as Property or Entity level.</param>
 /// <param name="source">The source of the error message, including the originating control and/or property name.</param>
 /// <param name="context">Context from which the error occurred.  This general property can be used as a container to keep track of the entity, for instance.</param>
 public ValidationSummaryItem(string message, string messageHeader, ValidationSummaryItemType itemType, ValidationSummaryItemSource source, object context)
 {
     this.MessageHeader = messageHeader;
     this.Message       = message;
     this.ItemType      = itemType;
     this.Context       = context;
     this.Sources       = new ObservableCollection <ValidationSummaryItemSource>();
     if (source != null)
     {
         this.Sources.Add(source);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Validates the current item.
        /// </summary>
        /// <param name="validateAllProperties">Whether or not to validate all properties.</param>
        /// <returns>Whether or not the current item is valid.</returns>
        private bool ValidateItem(bool validateAllProperties)
        {
            CancelEventArgs e = new CancelEventArgs();
            this.OnValidatingItem(e);

            if (e.Cancel)
            {
                return this.IsItemValid;
            }

            // Clear entity errors both from the ErrorSummary and the entity errors list.
            this.ClearEntityErrors();

            if (validateAllProperties)
            {
                // Check all input controls and validate them.
                this.UpdateAllSources();
            }

            if (this._lastItem != null)
            {
                // Check the rest of the parameters on the entity object.
                ValidationContext context = new ValidationContext(this._lastItem, null, null);
                List<ValidationResult> validationResults = new List<ValidationResult>();
                bool valid = Validator.TryValidateObject(this._lastItem, context, validationResults, validateAllProperties);

                if (!valid)
                {
                    Debug.Assert(validationResults.Count > 0, "Entity is not valid, but there are no errors.");
                    foreach (ValidationResult result in validationResults)
                    {
                        string errorMessage = result.ErrorMessage;
                        ValidationSummaryItem newError = new ValidationSummaryItem(errorMessage, null, ValidationSummaryItemType.ObjectError, null, null);

                        // Indicate that this DataForm is the owner of the error.  When clearing errors, only errors from this DataForm should be cleared.
                        newError.Context = this;
                        foreach (string property in result.MemberNames)
                        {
                            // Find a control matching this property name.
                            Control c = null;
                            if (this._templateBindingInfos != null)
                            {
                                foreach (DataFormBindingInfo bindingInfo in this._templateBindingInfos)
                                {
                                    if (bindingInfo.BindingExpression != null && bindingInfo.BindingExpression.ParentBinding != null &&
                                       bindingInfo.BindingExpression.ParentBinding.Path.Path == property)
                                    {
                                        c = bindingInfo.Element as Control;
                                        break;
                                    }
                                }
                            }
                            ValidationSummaryItemSource errorSource = new ValidationSummaryItemSource(property, c);
                            newError.Sources.Add(errorSource);
                        }

                        // Only add the errors that weren't picked up from the field-level validation.
                        if (this.EntityErrorShouldBeAdded(newError))
                        {
                            this._entityLevelErrors.Add(newError);

                            if (this.ValidationSummary != null)
                            {
                                Debug.Assert(this.ValidationSummary.Errors != null, "ValidationSummary.Errors should never be null.");

                                if (!this.ValidationSummary.Errors.Contains(newError))
                                {
                                    this.ValidationSummary.Errors.Add(newError);
                                }
                            }
                        }
                    }

                    this.SetIsItemValid();
                    return false;
                }

                validationResults.Clear();
            }

            this.SetIsItemValid();
            return this.IsItemValid;
        }
 private static int FindMatchingErrorSource(IList <ValidationSummaryItemSource> sources, ValidationSummaryItemSource sourceToFind)
 {
     if (sources != null)
     {
         for (int i = 0; i < sources.Count; i++)
         {
             if (sources[i].Equals(sourceToFind))
             {
                 return(i);
             }
         }
     }
     return(-1);
 }
        private void ExecuteClick(object sender)
        {
            ListBox lb = sender as ListBox;
            if (lb != null)
            {
                ValidationSummaryItem vsi = lb.SelectedItem as ValidationSummaryItem;
                if (vsi != null && this.FocusControlsOnClick)
                {
                    int idx;
                    // Ensure the currently selected item source is valid
                    if (vsi.Sources.Count == 0)
                    {
                        // Clear the current ESI source if the ESI has none, such as when the ESI has changed
                        this._currentValidationSummaryItemSource = null;
                    }
                    else
                    {
                        // If the current ESI source is not part of the current set, select the first one by default.
                        idx = FindMatchingErrorSource(vsi.Sources, this._currentValidationSummaryItemSource);
                        if (idx < 0)
                        {
                            this._currentValidationSummaryItemSource = vsi.Sources[0];
                        }
                    }

                    // Raise the event
                    FocusingInvalidControlEventArgs e = new FocusingInvalidControlEventArgs(vsi, this._currentValidationSummaryItemSource);
                    this.OnFocusingInvalidControl(e);

                    // Raise the AutomationPeer event
                    ValidationSummaryAutomationPeer peer = ValidationSummaryAutomationPeer.FromElement(this) as ValidationSummaryAutomationPeer;
                    if (peer != null && AutomationPeer.ListenerExists(AutomationEvents.InvokePatternOnInvoked))
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.InvokePatternOnInvoked);
                    }

                    // Focus the target, which will usually be the current ESI source or the overwritten one
                    if (!e.Handled && e.Target != null && e.Target.Control != null)
                    {
                        e.Target.Control.Focus();
                    }

                    // Update currently selected item, but only if there are multiple ESI sources.  
                    if (vsi.Sources.Count > 0)
                    {
                        idx = FindMatchingErrorSource(vsi.Sources, e.Target);
                        idx = idx < 0 ? 0 : ++idx % vsi.Sources.Count;
                        this._currentValidationSummaryItemSource = vsi.Sources[idx];
                    }
                }
            }
        }
 private static int FindMatchingErrorSource(IList<ValidationSummaryItemSource> sources, ValidationSummaryItemSource sourceToFind)
 {
     if (sources != null)
     {
         for (int i = 0; i < sources.Count; i++)
         {
             if (sources[i].Equals(sourceToFind))
             {
                 return i;
             }
         }
     }
     return -1;
 }