Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How ValidatesOnExceptions works

Tags:

validation

wpf

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

like image 440
MaRuf Avatar asked Dec 08 '11 17:12

MaRuf


1 Answers

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.

like image 84
nemesv Avatar answered Oct 04 '22 19:10

nemesv