MaRuf
MaRuf

Reputation: 1894

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 responsable to catch the custom exception and add it to Validation.Errors collection.

The problem is I have the same behavior even with ValidatesOnExceptions set to false

Could someone explain what I am missing?

Thanks

Upvotes: 6

Views: 8682

Answers (2)

Martin Horatschek
Martin Horatschek

Reputation: 260

It depends on the Version of the Framework you use.

In .NET 4 and 4.5 the answer of nemesv is correct. Still it differs form the MSDN documentation.

In .NET 3.5 when ValidatesOnExceptions was introduced, there was no default "red boarder" when the the DataConversion failed. If you want to have that you needed to explicitly add the ExceptionValidationRule into the ValidationRules of the Binding. The MSDN (3.5-4.5) says that you could simply set ValidatiosOnExceptions to true if you want a shorthand for that.

The problem is that the documentation in the version 4.0 and 4.5 is just wrong as it refers to the behaviour of the .NET 3.5

Upvotes: 4

nemesv
nemesv

Reputation: 139768

ValidatesOnExceptions is for custom exception display. But if you have TextBox bound to an int property then before the binding happen a conversion occurs which could cause "red border".

To try it out

<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; }
    }
}

Type a number to each textbox:

  1. Red border because of data conversion
  2. No red border becauseValidatesOnExceptions false
  3. Red border because ValidatesOnExceptions true

I hope it helped.

Upvotes: 5

Related Questions