Wpf – ValidatesOnExceptions fires but it doesn’t show the exception message

bindingvalidationwpf

I have a TextBox.TextProperty bound to a ViewModel property. In the binding I have explicitly set ValidatesOnExceptions to True.

In resources the TextBox has this trigger:

<Trigger Property="Validation.HasError" Value="true">
   <Setter 
      Property="ToolTip"
      Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
   <Setter TargetName="Border" Property="Background" Value="Crimson"/>

Unfortunately my implementation does not work perfectly because when I have an exception the TextBox background is highlighted with the Crimson color, but the tooltip text contains "Exception has been thrown by the target of an invocation." instead of the message I wrote in the exception constructor.

Do you have any suggestion?

Thank you in advance,
Marco

Best Solution

This is because the validation code is being invoked via reflection. Any caught exceptions will be wrapped up in a TargetInvocationException instance. The original exception will be stored as this exception's InnerException.


What happens if you bind to the ValidationError.Exception property instead of ValidationError.ErrorContext?