Freigeben über


FrameworkElement.BindingValidationError Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

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

Remarks

Use a handler based on EventHandler<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.

Examples

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" >
    <StackPanel.Resources>
        <my:Bills x:Name="MyBills"/>
    </StackPanel.Resources>
    <TextBox x:Name="MyTextBox" Width="50" Margin="10">
        <TextBox.Text>
            <Binding Mode="TwoWay" Source="{StaticResource MyBills}" 
                     Path="Amount" NotifyOnValidationError="true" 
                     ValidatesOnExceptions="true"/>
        </TextBox.Text>
    </TextBox>
    <Button Height="50" Width="150" Content="Click To Update Source"/>
</StackPanel>
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
private void StackPanel_BindingValidationError(object sender, 
    ValidationErrorEventArgs e)
{
    if (e.Action == ValidationErrorEventAction.Added)
    {
        MyTextBox.Background = new SolidColorBrush(Colors.Red);

    }
    else if (e.Action == ValidationErrorEventAction.Removed)
    {
        MyTextBox.Background = new SolidColorBrush(Colors.White);
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.