Greetings
It is necessary to make a simple check of the characters entered into several TextBox : firstly, that integers are entered, and secondly, that the entered numbers are in a certain interval. To do this, I decided to write my own simple class validator and connect to the markup. Actually XAML:
<TextBox IsEnabled="{Binding IsEnabledTextBoxs}"> <TextBox.Text> <Binding Path="Property"> <Binding.ValidationRules> <correct:MinMaxFormat_Correct Min="1" Max="6" /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> And class Validator:
class MinMaxFormat_Correct : ValidationRule { public int Min { get; set; } public int Max { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { var str = value as string; if (string.IsNullOrWhiteSpace(str)) { return new ValidationResult(false, "string is Empty or Equals null"); } try { var integ = Convert.ToInt32(str); if (integ <= Max && integ >= Min) { return new ValidationResult(true, null); } else { return new ValidationResult(false, "Property must be a minimum 1 and maximum 6"); } } catch (Exception ex) { return new ValidationResult(false, ex.Message); } } } Everything is correct - when you enter a string or an incorrect number, the text box is highlighted in red. But the problem is that the MinMaxFormat_Correct class has no effect on the VM model and its behavior is limited to simple highlighting. In other words - you need not only to highlight red textbox with an error but also to prohibit further user actions - for example, inside the model's VM there is a bool property to which the IsEnabled property of a button is attached, which will process the entered values and if the validator caught the exception, then bool= false and then not it is necessary to write a bunch of if-else in the handler of the button itself and immediately begin processing the data. Can something like this be done and if so how?
ValidationRulesets theValidationRuleattached property totrue. You can use it to determine the visibility of a button, for example. - Vlad