Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
 BindingValidationError Event

  Switch on low bandwidth view
.NET Framework Class Library for Silverlight
FrameworkElement..::.BindingValidationError Event

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

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Event BindingValidationError As EventHandler(Of ValidationErrorEventArgs)
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim handler As EventHandler(Of ValidationErrorEventArgs)

AddHandler instance.BindingValidationError, handler
C#
public event EventHandler<ValidationErrorEventArgs> BindingValidationError
XAML Attribute Usage
<frameworkElement BindingValidationError="eventhandler"/>

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 instead of or in addition to adjusting the specific input controls that have invalid input. If you choose this design of using a common validation routine and UI, make sure to not have the specific controls set Handled to true in their BindingValidationError handler. 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 choose to report such exceptions as an error to the object that has the property where the binding is applied. This is done 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 rather than 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>

Visual Basic
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

C#
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);
    }
}

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker