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?

  • This option does not work: click ? - Vlad
  • Here's another: www.stackoverflow.com/a/728835/218063 - Andrey NOP
  • @Vlad In that example, the class validator is not used at all. It turns out that it is impossible to expand its functionality in any way? - Sergey
  • one
    @Vlad Do you need to know about markup? Can it just hide the button? Just why, then, the rules of validation, if they have nothing to do in fact do not affect - introduced something wrong = nothing, we’ll scare red, but the work can be continued. - Sergey
  • one
    So ValidationRule sets the ValidationRule attached property to true . You can use it to determine the visibility of a button, for example. - Vlad

1 answer 1

Here is an example of how to hide a button.

Create some rule:

 public sealed class MyRule : ValidationRule { public override ValidationResult Validate(object value, CultureInfo cultureInfo) { var str = value as string; if (string.IsNullOrWhiteSpace(str)) { return new ValidationResult(false, "Error!"); } return new ValidationResult(true, null); } } 

Put a text box with a button in the markup. Bind the created rule to the text field. The button availability is controlled through a trigger, in which we check for errors in the text field.

 <StackPanel> <TextBox Name="textBox"> <Binding Path="Text" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:MyRule /> </Binding.ValidationRules> </Binding> </TextBox> <Button Content="Submit"> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="IsEnabled" Value="False"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=textBox, Path=(Validation.HasError)}" Value="false"> <Setter Property="IsEnabled" Value="True"/> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> 

UPD

For several TextBox you can use MultiDataTrigger :

 <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding ElementName=textBox, Path=(Validation.HasError)}" Value="false"/> <Condition Binding="{Binding ElementName=textBox2, Path=(Validation.HasError)}" Value="false"/> </MultiDataTrigger.Conditions> <Setter Property="IsEnabled" Value="True"/> </MultiDataTrigger> 
  • I see here binding to only one textbox. The question describes the situation for several. - Sergey
  • @ Sergey, updated the answer. - Vlad
  • Thank you very much! Good decision. - Sergey