The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.BindingValidationError Event

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Occurs when a data validation error is reported by a binding source.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Public Event BindingValidationError As EventHandler(Of ValidationErrorEventArgs)
<frameworkElement BindingValidationError="eventhandler"/>

Use a handler based on EventHandler(Of TEventArgs) and constrained by ValidationErrorEventArgs to handle this event.

The BindingValidationError event is a routed event. This means that if multiple BindingValidationError event handlers are registered for a sequence of objects connected by parent-child relationships in the object tree, the event is received by each object in that relationship. The event is raised by the object that directly receives the input condition that initiates the event, and then "bubbles" to each successive parent object. The bubbling metaphor indicates that the event starts at the bottom and works its way up the object tree. For a routed event, the sender available to the event handler identifies the object where the event is handled, not necessarily the object that actually received the input condition that initiated the event. To get the object that initiated the event, use the OriginalSource value of the event's ValidationErrorEventArgs event data.

The specific scenario for routing of a BindingValidationError event is that you might choose to have a single validation behavior or prompt in UI for a grouped set of individual controls that each validate their bound input. This is conceptually similar to how in typical Web page design there might be a single code path that processes all validation issues in an HTML form, and displays each validation error in a common format and UI location. If you choose this design of using a common validation routine and UI location, do not set Handled to true in control-specific BindingValidationError handlers. The grouping parent would be unable to run its own handler in response if Handled from events raised by child objects was already true.

A control author generally should neither raise nor handle this event in control design and control class implementation. Instead, the binding engine processes any exceptions that are thrown by a binding source when attempting to set the source in two-way binding from a control instance usage. On a per-binding basis, the binding can report such exceptions as an error to the object that has the property where the binding is applied. This behavior is enabled by setting both NotifyOnValidationError and ValidatesOnExceptions to true on the binding. A source setter exception when a two-way binding attempts to post invalid data to the source will then raise the BindingValidationError from the object.

The following examples shows the XAML UI that uses validation including the element where BindingValidationError handling is attached, and the validation handler code. Notice how the BindingValidationError handler is attached at the StackPanel level instead of on the individual text element being validated. The handler at the StackPanel level could conceivably perform validation on multiple child elements, although only one element is validated in this example.

<StackPanel BindingValidationError="StackPanel_BindingValidationError" >
        <my:Bills x:Name="MyBills"/>

        Text="To generate a validation error, input a negative number or a non-number and then tap the button."

    <TextBox x:Name="MyTextBox" Width="50" Margin="10">
            <Binding Mode="TwoWay" Source="{StaticResource MyBills}" 
                 Path="Amount" NotifyOnValidationError="true" 
    <Button Content="Tap to Update"/>

Private Sub StackPanel_BindingValidationError(ByVal sender As Object, _
    ByVal e As ValidationErrorEventArgs)

    If e.Action = ValidationErrorEventAction.Added Then

        MyTextBox.Background = New SolidColorBrush(Colors.Red)
    ElseIf e.Action = ValidationErrorEventAction.Removed Then
        MyTextBox.Background = New SolidColorBrush(Colors.White)
    End If

End Sub

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone