/// <summary> /// Sets the value. /// </summary> /// <param name="record">The record.</param> /// <param name="propName">Name of the prop.</param> /// <param name="value">The value.</param> /// <returns></returns> public virtual bool SetValue(object record, string propName, object value) { if (!(View is TreeGridSelfRelationalView)) { return(TreeGridHelper.SetValue(record, propName, value)); } var itemproperties = View.GetItemProperties(); if (itemproperties == null) { return(false); } if (itemaccessor.ContainsKey(propName)) { itemaccessor[propName].SetValue(record, value); return(true); } var tempitempproperties = itemproperties; var propertyNameList = propName.Split('.'); #if WPF return(PropertyDescriptorExtensions.SetComplexPropertyValue(propertyNameList, tempitempproperties, record, value)); #else return(PropertyInfoExtensions.SetComplexPropertyValue(propertyNameList, tempitempproperties, record, value)); #endif }
/// <summary> /// Gets the value. /// </summary> /// <param name="record">The record.</param> /// <param name="property">Name of the prop.</param> /// <returns></returns> /// public virtual object GetValue(object record, string property) { if (!(View is TreeGridSelfRelationalView)) { return(TreeGridHelper.GetValue(record, property)); } var itemproperties = View.GetItemProperties(); if (itemproperties == null || record == null) { return(null); } if (itemaccessor.ContainsKey(property)) { return(itemaccessor[property].GetValue(record)); } var tempitempproperties = itemproperties; var propertyNameList = property.Split('.'); #if WPF return(PropertyDescriptorExtensions.GetComplexPropertyValue(propertyNameList, tempitempproperties, record)); #else return(PropertyInfoExtensions.GetComplexPropertyValue(propertyNameList, tempitempproperties, record)); #endif }
/// <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> public static bool Validate(GridCell currentCell, string propertyName, object dataModel) { bool hasError = false; var itemproperties = currentCell.ColumnBase.GridColumn.DataGrid.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 = Syncfusion.Data.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 && !SyncfusionFramework3_5 || UWP hasError = !ValidateINotifyDataErrorInfo(currentCell, propertyName, dataModel); #endif return(!hasError); }
internal bool ValidateColumn(object rowData, string columnName, GridCell 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); } var itemproperties = this.dataGrid.View.GetItemProperties(); if (itemproperties == null) { return(isValid); } // WPF-25016 Using PropertyDescriptorExtensions for WPF and PropertyInfoExtensions for WinRT, the codes are cleaned up 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 !WinRT 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.dataGrid.Columns[propertyName] as GridColumn).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.SetCellValidationError(errorMessage, isAttributeError); } else { currentCell.RemoveCellValidationError(true); } } #if !SyncfusionFramework4_0 && !SyncfusionFramework3_5 || UWP if (rowData is INotifyDataErrorInfo) { isValid = isValid && DataValidation.ValidateINotifyDataErrorInfo(currentCell, columnName, rowData); } #endif return(isValid); }