How ValidatesOnExceptions Works

based on this example

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validatesonexceptions.aspx

It seems that ValidatesOnExceptions is responsible for catching a custom exception and adding it to the Validation.Errors collection.

Problem I have the same behavior even if ValidatesOnExceptions is set to false

Can someone explain what I am missing?

thanks

+4
source share
2 answers

ValidatesOnExceptions intended to display custom exceptions. But if you have a TextBox associated with the int property, then before the binding occurs, a conversion will occur that can cause a "red border".

To try

 <TextBox Text="{Binding IntField}"/> <TextBox Text="{Binding StringField, ValidatesOnExceptions=False}"/> <TextBox Text="{Binding StringField, ValidatesOnExceptions=True}"/> public int IntField { get; set; } private string stringField; public string StringField { get { return stringField; } set { throw new Exception(); text = stringField; } } } 

Enter a number for each text field:

  • Red border due to data conversion
  • There is no red border because ValidatesOnExceptions is false
  • Red border, because ValidatesOnExceptions are true,

I hope this helps.

+3
source

It depends on the version of the structure you are using.

In .NET 4 and 4.5, nemesv's answer is correct. However, it differs from the MSDN documentation.

In .NET 3.5, when ValidatesOnExceptions was introduced, there was no โ€œred borderโ€ by default when DataConversion failed. If you want you to explicitly add an ExceptionValidationRule to the ValidationRules Binding . MSDN (3.5-4.5) says that you can just set ValidatiosOnExceptions to true if you want to shorten this.

The problem is that the documentation in versions 4.0 and 4.5 is simply incorrect, as it relates to the behavior of .NET 3.5

+1
source

Source: https://habr.com/ru/post/1385388/


All Articles