Exporter (0) Imprimer
Développer tout

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)

public event EventHandler<ValidationErrorEventArgs> BindingValidationError
<frameworkElement BindingValidationError="eventhandler"/>

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.

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


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

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

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

Ajouts de la communauté

AJOUTER
Microsoft réalise une enquête en ligne pour recueillir votre opinion sur le site Web de MSDN. Si vous choisissez d’y participer, cette enquête en ligne vous sera présentée lorsque vous quitterez le site Web de MSDN.

Si vous souhaitez y participer,
Afficher:
© 2014 Microsoft