Print Page | Close Window

WPF Data Verification Errors

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1063
Printed Date: 12-May-2026 at 10:37pm


Topic: WPF Data Verification Errors
Posted By: mbevins1
Subject: WPF Data Verification Errors
Date Posted: 20-Jan-2009 at 5:05am

I am working on trying to get the data verification to work with WPF and cannot seem to figure out how to use the verification built into DevForce.  I currently added the below code to my app.xaml and am getting the notices of errors however, the error messages are not correct.

On a field that is required, I am getting this error:
Exception has been thrown by the target of an invocation.

Then on a field that is not required and left blank, I am getting this error:
Input string was not in a correct format.

Are there any examples on how to use the built in DevForce verification with WPF?

 

Here is what I have in my app file:

        <ControlTemplate x:Key="validationTemplate">

            <DockPanel>

                <TextBlock Margin="5,0,5,0" Foreground="Red" FontSize="16" VerticalAlignment="Center" Text="*" ToolTip="{Binding ElementName=errorAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />

                <AdornedElementPlaceholder x:Name="errorAdorner" />

            </DockPanel>

        </ControlTemplate>

 

        <Style TargetType="{x:Type TextBox}">

            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource validationTemplate}" />

        </Style>

 

Here is an example of one of my textboxes:

            <TextBox Grid.Column="1" Grid.Row="5" Text="{Binding CurrentVehicle.Description, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="Auto" Width="Auto" />




Replies:
Posted By: GregD
Date Posted: 26-Jan-2009 at 12:33pm

>> Exception has been thrown by the target of an invocation.

Here’s some text from the discussion of WPF validation in Matthew MacDonald’s book on WPF that may be relevant (p.543):

“If you’re using custom validation rules, you’ll almost certainly choose to place the error information in the ValidationError.ErrorContent propety. If you’re using the ExceptionValidationRule, the ErrorContent property will return the Message property of the corresponding exception. However, there’s a catch. If an exception occurs because the data type cannot be cast to the approprate value, the ErrorContent works as expected and reports the problem. However, if the property setter in the data object throws an exception, this exception is wrapped in a TargetInvocationException, and the ErrorContent provides the text from the TargetInvocationException.Message property, which is the much less helpful warning ‘Exception has been thrown by the target of an invocation.

“Thus, if you’re using your property setters to raise exceptions, you’ll need to add code that checks the InnerException property of the TargetInvocationException. If it’s not null, you can retrieve the original exception object and use its Message property instead of the ValidationError.ErrorContent property.”

 
 
>> Then on a field that is not required and left blank, I am getting this error: Input string was not in a correct format.
 
Don’t have an answer for you on that one, except that I think this error isn’t resulting from an exception thrown by the property setter, but by something at a higher level, maybe associated with a validation defined at the binding level?



Posted By: mbevins1
Date Posted: 27-Jan-2009 at 9:40am

Greg, are there any examples on how to properly use devforce validation with WPF?



Posted By: GregD
Date Posted: 27-Jan-2009 at 11:35am
Originally posted by mbevins1

Greg, are there any examples on how to properly use devforce validation with WPF?

 
Working on one right now, Mike. Coming soon!


Posted By: lars-erik
Date Posted: 30-Jan-2009 at 1:45am

Yes, mee to - I would really like to see an example with WPF validation.

Can you also add an example with validation in  Validation Application Block (VAB)  from Enterprise library 4 ?



Print Page | Close Window