Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/how-to-implement-binding-validation.md
T
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

3.9 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Implement Binding Validation Learn how to using binding validation to provide visual feedback to the user when an invalid value is entered in Windows Presentation Foundation (WPF). 03/30/2017
csharp
vb
validation of binding [WPF]
data binding [WPF], validation of binding
binding [WPF], validation of
eb98b33d-9866-49ae-b981-bc5ff20d607a

How to: Implement Binding Validation

This example shows how to use an xref:System.Windows.Controls.Validation.ErrorTemplate%2A and a style trigger to provide visual feedback to inform the user when an invalid value is entered, based on a custom validation rule.

Example

The text content of the xref:System.Windows.Controls.TextBox in the following example is bound to the Age property (of type int) of a binding source object named ods. The binding is set up to use a validation rule named AgeRangeRule so that if the user enters non-numeric characters or a value that is smaller than 21 or greater than 130, a red exclamation mark appears next to the text box and a tool tip with the error message appears when the user moves the mouse over the text box.

[!code-xamlBindValidation#2]

The following example shows the implementation of AgeRangeRule, which inherits from xref:System.Windows.Controls.ValidationRule and overrides the xref:System.Windows.Controls.ValidationRule.Validate%2A method. The Int32.Parse method is called on the value to make sure that it does not contain any invalid characters. The xref:System.Windows.Controls.ValidationRule.Validate%2A method returns a xref:System.Windows.Controls.ValidationResult that indicates if the value is valid based on whether an exception is caught during the parsing and whether the age value is outside of the lower and upper bounds.

[!code-csharpBindValidation#3] [!code-vbBindValidation#3]

The following example shows the custom xref:System.Windows.Controls.ControlTemplate validationTemplate that creates a red exclamation mark to notify the user of a validation error. Control templates are used to redefine the appearance of a control.

[!code-xamlBindValidation#4]

As shown in the following example, the xref:System.Windows.Controls.ToolTip that shows the error message is created using the style named textBoxInError. If the value of xref:System.Windows.Controls.Validation.HasError%2A is true, the trigger sets the tool tip of the current xref:System.Windows.Controls.TextBox to its first validation error. The xref:System.Windows.Data.Binding.RelativeSource%2A is set to xref:System.Windows.Data.RelativeSourceMode.Self, referring to the current element.

[!code-xamlBindValidation#5]

For the complete example, see Bind Validation sample.

Note that if you do not provide a custom xref:System.Windows.Controls.Validation.ErrorTemplate%2A the default error template appears to provide visual feedback to the user when there is a validation error. See "Data Validation" in Data Binding Overview for more information. Also, [!INCLUDETLA2#tla_winclient] provides a built-in validation rule that catches exceptions that are thrown during the update of the binding source property. For more information, see xref:System.Windows.Controls.ExceptionValidationRule.

See also