Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionValidationRule doesn't react to exceptions

I have an ExceptionValidationRule on my TextBox:

<Window.Resources>
    <Style x:Key="textStyleTextBox" TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<TextBox x:Name="myTextBox"
    {Binding Path=MyProperty, ValidatesOnExceptions=True}"
    Style="{StaticResource ResourceKey=textStyleTextBox}" />

and MyProperty looks like that:

private int myProperty;

public int MyProperty
{
    get { return myProperty; }
    set
    {
        if(value > 10)
            throw new ArgumentException("LOL that's an error");
        myProperty = value;
    }
}

In DEBUG mode, application crashes with unhandled exception "LOL that's an error" (WPF Binding Engine doesn't catch this and I think it should...).

In RELEASE mode, everything works fine.

Can someone tell me, why the hell is this happening? And how can I fix this?

like image 503
Darmak Avatar asked Apr 30 '10 21:04

Darmak


1 Answers

The solution is not so obvious nor well documented, but simple enough. The reason Visual Studio breaks for exceptions when running in debug mode is because it's configured that way.

In the Debug menu, select "Exceptions...". In this dialog you control how VS handles exceptions. Simply uncheck "User-unhandled" for "Common Language Runtime Exceptions", press OK and run your project again.

like image 133
Reyhn Avatar answered Sep 28 '22 06:09

Reyhn