/// <summary>
        /// Validates the value of current cell.
        /// </summary>
        /// <param name="currentCell">
        /// The current cell that is to validated.
        /// </param>
        /// <param name="propertyName">
        /// The propertyName.
        /// </param>
        /// <param name="dataModel">
        /// The dataModel.
        /// </param>
        /// <returns>
        /// <b>true</b> if the validation is successful; otherwise, <b>false</b>.
        /// </returns>
        internal static bool Validate(TreeGridCell currentCell, string propertyName, object dataModel)
        {
            bool hasError       = false;
            var  itemproperties = currentCell.ColumnBase.TreeGridColumn.TreeGrid.View.GetItemProperties();

            // WPF-25016 Using PropertyDescriptorExtensions for WPF and PropertyInfoExtensions for WinRT, the codes are cleaned up
            if (propertyName.Contains('.'))
            {
                var propNames = propertyName.Split('.');
                propertyName = propNames[propNames.Length - 1];
                Array.Resize(ref propNames, propNames.Length - 1);
                var pName = string.Join(".", propNames);
#if WPF
                dataModel = PropertyDescriptorExtensions.GetValue(itemproperties, dataModel, pName);
#else
                dataModel = PropertyInfoExtensions.GetValue(itemproperties, dataModel, pName);
#endif
            }

            if (dataModel == null)
            {
                return(hasError);
            }

#if WPF
            var dataValidation = dataModel as IDataErrorInfo;
            if (dataValidation != null)
            {
                string errormessage = dataValidation[propertyName];
                hasError = !String.IsNullOrEmpty(errormessage);
                if (hasError)
                {
                    currentCell.bindingErrorMessage = errormessage;
                }

                currentCell.ApplyValidationVisualState();
                return(!hasError);
            }
#endif
#if !SyncfusionFramework4_0 || UWP
            hasError = !ValidateINotifyDataErrorInfo(currentCell, propertyName, dataModel);
#endif
            return(!hasError);
        }
        internal static bool ValidateINotifyDataErrorInfo(TreeGridCell currentCell, string propertyName, object dataModel)
        {
            bool hasError            = false;
            var  dataErrorValidation = dataModel as INotifyDataErrorInfo;

            currentCell.bindingErrorMessage = string.Empty;
            if (dataErrorValidation != null)
            {
                var errorList = dataErrorValidation.GetErrors(propertyName);
                if (errorList != null)
                {
                    var errorMessage = errorList.Cast <string>().FirstOrDefault();
                    hasError = !String.IsNullOrEmpty(errorMessage);
                    if (hasError)
                    {
                        currentCell.bindingErrorMessage = errorMessage;
                    }
                }
                currentCell.ApplyValidationVisualState();
            }
            return(!hasError);
        }
Exemplo n.º 3
0
        internal bool ValidateColumn(object rowData, string columnName, TreeGridCell currentCell, RowColumnIndex currentCellIndex)
        {
            var  propertyName     = columnName;
            bool isValid          = true;
            var  errorMessage     = string.Empty;
            bool isAttributeError = false;

            if (rowData == null || string.IsNullOrEmpty(columnName) || currentCell == null || currentCellIndex == RowColumnIndex.Empty)
            {
                return(isValid);
            }
#if UWP
            PropertyInfoCollection itemProperties;
#else
            PropertyDescriptorCollection itemProperties;
#endif
            if (this.treeGrid.HasView)
            {
                itemProperties = this.treeGrid.View.GetItemProperties();
                if (itemProperties == null)
                {
                    return(isValid);
                }
            }
            else
            {
#if WPF
                itemProperties = TypeDescriptor.GetProperties(rowData.GetType());
#else
                itemProperties = new PropertyInfoCollection(rowData.GetType());
#endif
            }

            if (columnName.Contains('.'))
            {
                var propNames = columnName.Split('.');
                columnName = propNames[propNames.Length - 1];
                Array.Resize(ref propNames, propNames.Length - 1);
                var pName = string.Join(".", propNames);
#if WPF
                rowData = PropertyDescriptorExtensions.GetValue(itemProperties, rowData, pName);
#else
                rowData = Syncfusion.Data.PropertyInfoExtensions.GetValue(itemProperties, rowData, pName);
#endif
            }

            PropertyInfo      propertyinfo      = null;
            ValidationContext validationContext = null;
#if WPF
            if (rowData != null)
            {
                propertyinfo      = rowData.GetType().GetProperty(columnName);
                validationContext = new ValidationContext(rowData, null, null)
                {
                    MemberName = columnName
                };
            }
#else
            if (rowData != null)
            {
                propertyinfo      = rowData.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == columnName);
                validationContext = new ValidationContext(rowData)
                {
                    MemberName = columnName
                };
            }
#endif
            if (errorMessages != null && rowIndex == currentCellIndex.RowIndex && errorMessages.Keys.Contains(columnName) && IsCurrentRowValidated)
            {
                errorMessage = errorMessages[columnName];
            }
            if ((this.treeGrid.Columns[propertyName] as TreeGridColumn).GridValidationMode != GridValidationMode.None)
            {
                if (propertyinfo != null)
                {
                    var validationAttribute = propertyinfo.GetCustomAttributes(false).OfType <ValidationAttribute>();
                    var value   = propertyinfo.GetValue(rowData, null);
                    var results = new List <System.ComponentModel.DataAnnotations.ValidationResult>();
                    try
                    {
                        if (!Validator.TryValidateValue(value, validationContext, results, validationAttribute))
                        {
                            foreach (var result in results)
                            {
                                errorMessage = !string.IsNullOrEmpty(errorMessage) ? errorMessage + string.Format("\n") + result.ErrorMessage : errorMessage + result.ErrorMessage;
                            }
                            isValid          = false;
                            isAttributeError = true;
                        }
                    }
                    catch (Exception e)
                    {
                        errorMessage     = e.Message;
                        isValid          = false;
                        isAttributeError = true;
                    }
                }
            }

            if (currentCell != null)
            {
                if (!isValid || !string.IsNullOrEmpty(errorMessage))
                {
                    currentCell.SetError(errorMessage, isAttributeError);
                }
                else
                {
                    currentCell.RemoveError(true);
                }
            }
#if !SyncfusionFramework4_0 || UWP
            if (rowData is INotifyDataErrorInfo)
            {
                isValid = isValid && TreeGridDataValidation.ValidateINotifyDataErrorInfo(currentCell, columnName, rowData);
            }
#endif
            return(isValid);
        }