Пример #1
0
        internal bool RaiseCellValidate(RowColumnIndex currentCellIndex, ITreeGridCellRenderer renderer, bool allowattributeValidation)
        {
            if (IsCurrentCellValidated)
            {
                return(true);
            }

            var dataRow = treeGrid.RowGenerator.Items.FirstOrDefault(item => item.RowIndex == currentCellIndex.RowIndex);

            if (dataRow == null)
            {
                return(false);
            }
            var columnBase = dataRow.VisibleColumns.FirstOrDefault(x => x.ColumnIndex == currentCellIndex.ColumnIndex);

            if (columnBase == null)
            {
                return(false);
            }
            renderer = renderer ?? columnBase.Renderer;

            if (renderer == null)
            {
                return(false);
            }

            //WPF-36608 - CurrenctCellvalidating event are not fired, when we select cell on treeGrid template column.
            if (!columnBase.Renderer.CanValidate())
            {
                SetCurrentCellValidated(true);
                return(true);
            }
            object oldValue = null;
            object changedNewValue;
            string errorMessage;

            oldValue = this.treeGrid.SelectionController.CurrentCellManager.oldCellValue;
            var newCellValue = renderer.GetControlValue();
            var treeNode     = treeGrid.View.Nodes.GetNode(dataRow.RowData);

            if (this.RaiseCurrentCellValidatingEvent(oldValue, newCellValue, columnBase.TreeGridColumn, out changedNewValue, currentCellIndex, columnBase.ColumnElement as FrameworkElement, out errorMessage, dataRow.RowData, treeNode))
            {
                bool isValid = true;
                if (newCellValue != changedNewValue)
                {
                    renderer.SetControlValue(changedNewValue);
                }
                if (allowattributeValidation)
                {
                    if ((columnBase.ColumnElement is TreeGridCell) && (columnBase.ColumnElement as TreeGridCell).Content != null && (columnBase.ColumnElement as TreeGridCell).Content is FrameworkElement)
                    {
                        renderer.UpdateSource((columnBase.ColumnElement as TreeGridCell).Content as FrameworkElement);
                    }
                    if (columnBase.TreeGridColumn.GridValidationMode != GridValidationMode.None)
                    {
                        isValid = this.treeGrid.ValidationHelper.ValidateColumn(dataRow.RowData, columnBase.TreeGridColumn.MappingName, (columnBase.ColumnElement as TreeGridCell), currentCellIndex) &&
                                  TreeGridDataValidation.Validate((columnBase.ColumnElement as TreeGridCell), columnBase.TreeGridColumn.MappingName, columnBase.ColumnElement.DataContext);
                    }
#if WPF
                    if (!isValid && columnBase.TreeGridColumn.GridValidationMode == GridValidationMode.InEdit)
                    {
                        return(false);
                    }
#endif
                }

                this.RaiseCurrentCellValidatedEvent(oldValue, columnBase.Renderer.GetControlValue(), columnBase.TreeGridColumn, errorMessage, dataRow.RowData, treeNode);
                SetCurrentCellValidated(true);
                return(true);
            }
            return(false);
        }
Пример #2
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);
        }
Пример #3
0
        internal void ApplyRowHeaderVisualState()
        {
            if (!TreeGrid.showRowHeader || this.RowType == TreeRowType.HeaderRow)
            {
                return;
            }

            var columnBase = this.VisibleColumns.FirstOrDefault(col => col.ColumnType == TreeColumnType.RowHeader);

            if (columnBase == null)
            {
                return;
            }

            var rowHeaderCell = columnBase.ColumnElement as TreeGridRowHeaderCell;

            if (rowHeaderCell == null)
            {
                return;
            }
            if (this.IsCurrentRow)
            {
                rowHeaderCell.State = "CurrentRow";
            }
            else
            {
                rowHeaderCell.State = "Normal";
            }
            if (this.IsEditing)
            {
                rowHeaderCell.State = "EditingRow";
            }
            rowHeaderCell.ApplyVisualState();

            var dataValidation = this.RowData;

            if (dataValidation != null)
            {
#if !SyncfusionFramework4_0 || UWP
                if ((dataValidation as INotifyDataErrorInfo) != null)
                {
                    if (DataValidation.ValidateRowINotifyDataErrorInfo(this.RowData))
                    {
                        rowHeaderCell.RowErrorMessage = GridResourceWrapper.RowErrorMessage;
                        if (!rowHeaderCell.State.Equals("Normal"))
                        {
                            rowHeaderCell.State = "Error_CurrentRow";
                        }
                        else
                        {
                            rowHeaderCell.State = "Error";
                        }
                        rowHeaderCell.ApplyVisualState();
                        return;
                    }
                }
#endif
#if WPF
                if ((dataValidation as IDataErrorInfo) != null)
                {
                    if (TreeGridDataValidation.ValidateRow(this.RowData))
                    {
                        rowHeaderCell.RowErrorMessage = (dataValidation as IDataErrorInfo).Error;
                        if (!rowHeaderCell.State.Equals("Normal"))
                        {
                            rowHeaderCell.State = "Error_CurrentRow";
                        }
                        else
                        {
                            rowHeaderCell.State = "Error";
                        }
                        rowHeaderCell.ApplyVisualState();
                        return;
                    }
                    else
                    {
                        if (this.TreeGrid.View != null && this.TreeGrid.View.CurrentItem == null)
                        {
                            rowHeaderCell.State = "Normal";
                        }
                    }
                }
#endif
            }
            rowHeaderCell.ApplyVisualState();
        }