Search This Blog

Thursday, September 22, 2011

Silverlight Data Validation in controls

Overall validation description

In the View we set ValidatesOnExceptions=True.

<TextBox Name ="IntValueTextBox" Text="{Binding IntValue, Mode=TwoWay, ValidatesOnExceptions=True}">

In ViewModel the property that we want bind to and want to validate looks as follows:


[System.Diagnostics.DebuggerHidden()]
public string IntValue
{
    get { return intValue.ToString(); }
    [System.Diagnostics.DebuggerHidden()]
    set
    {
        int temp;
        if (!Int32.TryParse(value, out temp))
        {
            throw new Exception("Please enter a valid integer value");
        }
        else
        {
            intValue = Int32.Parse(value);
        }
    }
}
private int intValue;

Hiding validation exception for Visual Studio Debugger

While using validation (get;set; property approach) validation expceptions are thrown as follows:

throw new Exception("Please enter a valid integer value");

It causes quite disturbing effect of receiving errors in Visual Studio debugger. When using:

[System.Diagnostics.DebuggerHidden()]

in every property that we validate and its get; set; methods we will hide these error from the debugger.

No comments:

Post a Comment

If you like this post, please leave a comment :)