I have created a validation rule to validate that the value in a textbox is within a certain range.
When the value cannot be converted, the error "wrong format!" shows well. But when the value is out of range, even though I have put a break point that proves that the other invalid result ("out of range") is actually reached, it does not show as an error.Code:class ValueValidationRule : ValidationRule { private int _min = 0; private int _max = 0; public ValueValidationRule(int min, int max) { _min = min; _max = max; } public override ValidationResult Validate(object source, CultureInfo info) { int value = 0; try { value = Convert.ToInt32(source); if (value >= _min && value <= _max) { return ValidationResult.ValidResult; } else { return new ValidationResult(false, "out of range"); } } catch (Exception) { return new ValidationResult(false, "wrong format!"); } } }
Here is the binding:
Code:Binding testbind = new Binding(); testbind .Path = new PropertyPath("Value"); testbind .UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; testbind .ValidationRules.Add(new ValueValidationRule(1, 10)); testbind .NotifyOnValidationError = true; testbind .TargetNullValue = 0; codeBinding.SetBinding(TextBox.TextProperty, testbind);


Reply With Quote
Bookmarks