// Ensure that an item over $100 is available for at least 7 days. public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bg = value as BindingGroup; // Get the source object. PurchaseItem item = bg.Items[0] as PurchaseItem; object doubleValue; object dateTimeValue; // Get the proposed values for Price and OfferExpires. bool priceResult = bg.TryGetValue(item, "Price", out doubleValue); bool dateResult = bg.TryGetValue(item, "OfferExpires", out dateTimeValue); if (!priceResult || !dateResult) { return(new ValidationResult(false, "Properties not found")); } double price = (double)doubleValue; DateTime offerExpires = (DateTime)dateTimeValue; // Check that an item over $100 is available for at least 7 days. if (price > 100) { if (offerExpires < DateTime.Today + new TimeSpan(7, 0, 0, 0)) { return(new ValidationResult(false, "Items over $100 must be available for at least 7 days.")); } } return(ValidationResult.ValidResult); }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bg = (BindingGroup)value; ZahnerXandY xy = (ZahnerXandY)bg.Items[0]; object objValue = null; if (!bg.TryGetValue(xy, "NumX", out objValue)) { return(new ValidationResult(false, "NumX is null.")); } int NumX; if (!Int32.TryParse(objValue as string, out NumX)) { return(new ValidationResult(false, "NumX isn't a whole number.")); } if (!bg.TryGetValue(xy, "NumY", out objValue)) { return(new ValidationResult(false, "NumY is null.")); } int NumY; if (!Int32.TryParse(objValue as string, out NumY)) { return(new ValidationResult(false, "NumY isn't a whole number.")); } if (NumX >= NumY + 20 || NumX <= NumY - 20) { return(new ValidationResult(true, "")); } else { return(new ValidationResult(false, "not 20 apart.")); } /* * * int numX = int.Parse(tbNumX.ToString()); * int numY = int.Parse(tbNumY.ToString()); * */ }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { // 特定のフィールド値ではなく, BindingGroup が入ってくる BindingGroup bg = value as BindingGroup; if (bg == null) { return(new ValidationResult(false, "internal error: BindingGroup object is invalid.")); } if (bg.Items.Count != 1) { return(new ValidationResult(false, "internal error: BindingGroup.Items invalid.")); } // Get the source object. MyViewModel data = bg.Items[0] as MyViewModel; if (data == null) { return(new ValidationResult(false, "internal error: Data is not a MyViewModel.")); } object combo1, combo2; // POINT!: Commit される前のデータを得る. bool r1 = bg.TryGetValue(data, "Combo1", out combo1); bool r2 = bg.TryGetValue(data, "Combo2", out combo2); if (!r1 || !r2) { return(new ValidationResult(false, "internal error: Properties not found")); } if (Convert.ToInt32(combo1) == Convert.ToInt32(combo2)) { return(new ValidationResult(false, "Combo1 & 2 must not be same")); } // OK. return(ValidationResult.ValidResult); }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) { string result = string.Empty; BindingGroup bindingGroup = (BindingGroup)value; foreach (var item in bindingGroup.Items.OfType <T>()) { Type type = item.GetType(); // First we find all properties that have attributes. foreach (var propertyInfo in type.GetProperties()) { foreach (var attribute in Attribute.GetCustomAttributes(propertyInfo, true)) { // We continue if we found a data annotation if (attribute is System.ComponentModel.DataAnnotations.ValidationAttribute validationAttribute) { if (bindingGroup.TryGetValue(item, propertyInfo.Name, out object itemValue)) { if (itemValue == DependencyProperty.UnsetValue) { itemValue = null; } // We set the error message if the validation of the property fails. if (!validationAttribute.IsValid(itemValue)) { if (!string.IsNullOrWhiteSpace(result)) { result += Environment.NewLine; } if (string.IsNullOrEmpty(validationAttribute.ErrorMessage)) { result += string.Format("Validation on {0} failed!", propertyInfo.Name); } else { result += validationAttribute.ErrorMessage; } } } } } } } if (!string.IsNullOrWhiteSpace(result)) { return(new ValidationResult(false, result)); } else { return(ValidationResult.ValidResult); } }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bindingGroup = (BindingGroup)value; if (bindingGroup.Items.Count > 0) { AbsM.GroupM group = bindingGroup.Items[0] as AbsM.GroupM; if (group != null) { object groupText = null; bindingGroup.TryGetValue(group, "Text", out groupText); if (!String.IsNullOrEmpty((String)(groupText))) { return(ValidationResult.ValidResult); } else { return(new ValidationResult(false, "Название группы в пользовательском интерфейсе не должно быть пустым")); } } } return(ValidationResult.ValidResult); }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bindingGroup = (BindingGroup)value; if (bindingGroup.Items.Count > 0) { RasM.RastTableBaseM rastrLayer = (RasM.RastTableBaseM)(bindingGroup.Items[0]); RasVM.RastrDataRepositoryVM rastrRepo = rastrLayer.Source as RasVM.RastrDataRepositoryVM; Object filePathObject = null; if (bindingGroup.TryGetValue(rastrLayer, "FilePath", out filePathObject)) { String filePath = Convert.ToString(filePathObject); bool fileExists = false; fileExists = filePath != "" && System.IO.File.Exists(filePath); if (!fileExists) { if (filePath.StartsWith(".")) { filePath = System.Windows.Forms.Application.StartupPath + "\\" + filePath.TrimStart(new char[] { '.', '\\' }); if (System.IO.File.Exists(filePath)) { fileExists = true; } } } if (!fileExists) { return(new ValidationResult(false, "Путь к файлу задан неправильно")); } else { foreach (var iTable in rastrRepo.Tables) { RasM.RastTableBaseM rastrTable = iTable as RasM.RastTableBaseM; if (rastrTable != rastrLayer && rastrTable.FilePath == filePath) { return(new ValidationResult(false, "В системе уже присутствует растровый слой который указывает на этот файл")); } } } } else { return(new ValidationResult(false, "Не задано свойство \"Путь к файлу\"")); } String rastrName = (String)bindingGroup.GetValue(rastrLayer, "Text"); if (String.IsNullOrEmpty(rastrName.Trim())) { return(new ValidationResult(false, "Не задано название растрового слоя")); } bool rastrNameExists = (from AbsM.TableBaseM rastrTable in rastrRepo.Tables where (rastrTable.Text == rastrName && rastrLayer != rastrTable) select rastrTable).Count() > 0; if (rastrNameExists) { return(new ValidationResult(false, "Слой с таким названием уже существует")); } BooleanYesNoConverter boolYesNo = new BooleanYesNoConverter(); bool useBounds = Convert.ToBoolean(boolYesNo.ConvertBack(bindingGroup.GetValue(rastrLayer, "UseBounds"), null, null, null)); object minScaleObj = bindingGroup.GetValue(rastrLayer, "MinScale"); object maxScaleObj = bindingGroup.GetValue(rastrLayer, "MaxScale"); int minScale = -1; int maxScale = -1; try { minScale = Convert.ToInt32(minScaleObj); } catch (Exception ex) { String mes = "Ошибка в значении нижней границы масштаба: \n" + ex.Message; return(new ValidationResult(false, mes)); } try { maxScale = Convert.ToInt32(maxScaleObj); } catch (Exception ex) { String mes = "Ошибка в значении верхней границы масштаба: \n" + ex.Message; return(new ValidationResult(false, mes)); } if (useBounds) { if (minScale < 0) { return(new ValidationResult(false, "Минимальное значение не может быть отрицательным числом")); } if (maxScale < 0) { return(new ValidationResult(false, "Максимальное значение не может быть отрицательным числом")); } if (minScale > maxScale) { return(new ValidationResult(false, "Минимальное значение не может быть больше максимального")); } } return(ValidationResult.ValidResult); } else { return(new ValidationResult(false, "Не выбран объект для отображения")); } }