--- title: "How to: Implement Binding Validation" description: Learn how to using binding validation to provide visual feedback to the user when an invalid value is entered in Windows Presentation Foundation (WPF). ms.date: 03/30/2017 dev_langs: - csharp - vb helpviewer_keywords: - "validation of binding [WPF]" - "data binding [WPF], validation of binding" - "binding [WPF], validation of" ms.assetid: eb98b33d-9866-49ae-b981-bc5ff20d607a --- # How to: Implement Binding Validation This example shows how to use an 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 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-xaml[BindValidation#2](~/samples/snippets/csharp/VS_Snippets_Wpf/BindValidation/CSharp/Window1.xaml#2)] The following example shows the implementation of `AgeRangeRule`, which inherits from and overrides the method. The `Int32.Parse` method is called on the value to make sure that it does not contain any invalid characters. The method returns a 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-csharp[BindValidation#3](~/samples/snippets/csharp/VS_Snippets_Wpf/BindValidation/CSharp/AgeRangeRule.cs#3)] [!code-vb[BindValidation#3](~/samples/snippets/visualbasic/VS_Snippets_Wpf/BindValidation/VisualBasic/AgeRangeRule.vb#3)] The following example shows the custom `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-xaml[BindValidation#4](~/samples/snippets/csharp/VS_Snippets_Wpf/BindValidation/CSharp/Window1.xaml#4)] As shown in the following example, the that shows the error message is created using the style named `textBoxInError`. If the value of is `true`, the trigger sets the tool tip of the current to its first validation error. The is set to , referring to the current element. [!code-xaml[BindValidation#5](~/samples/snippets/csharp/VS_Snippets_Wpf/BindValidation/CSharp/Window1.xaml#5)] For the complete example, see [Bind Validation sample](https://github.com/Microsoft/WPF-Samples/tree/master/Data%20Binding/BindValidation). Note that if you do not provide a custom 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](data-binding-overview.md) for more information. Also, WPF provides a built-in validation rule that catches exceptions that are thrown during the update of the binding source property. For more information, see . ## See also - [Data Binding Overview](data-binding-overview.md) - [How-to Topics](data-binding-how-to-topics.md)