diff --git a/CS/MainWindow.xaml b/CS/MainWindow.xaml index 4e1bb01..0e965ff 100644 --- a/CS/MainWindow.xaml +++ b/CS/MainWindow.xaml @@ -10,6 +10,9 @@ + + + diff --git a/CS/MainWindow.xaml.cs b/CS/MainWindow.xaml.cs index c13cca5..21aad52 100644 --- a/CS/MainWindow.xaml.cs +++ b/CS/MainWindow.xaml.cs @@ -51,15 +51,17 @@ void RaisePropertyChanged(string property) { string IDataErrorInfo.Error { get { return GetError(); } } + string GetError() { if (string.IsNullOrEmpty(TestString)) - return "ErrorType=Critical;ErrorContent=empty"; + return "ErrorType=Critical;ErrorContent=The value is not provided. Please enter a value"; if (TestString.Length < 3) - return "ErrorType=Critical;ErrorContent=error"; + return "ErrorType=Warning;ErrorContent=The value is less than 3 characters. Please enter at least 5 characters"; if (TestString.Length < 5) - return "ErrorType=Information;ErrorContent=warning"; + return "ErrorType=Information;ErrorContent=The value is less than 5 characters. Please enter at least 5 characters"; return string.Empty; } + string IDataErrorInfo.this[string columnName] { get { if (columnName == "TestString") diff --git a/Images/validation.jpg b/Images/validation.jpg new file mode 100644 index 0000000..9a38ef7 Binary files /dev/null and b/Images/validation.jpg differ diff --git a/Readme.md b/Readme.md index 368efed..1bc045d 100644 --- a/Readme.md +++ b/Readme.md @@ -4,25 +4,141 @@ [![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183) [![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives) - -*Files to look at*: + +# WPF Editors - Indicate Errors and Warnings by Implementing IDataErrorInfo + +This example validates input in a WPF [`TextEdit`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Editors.TextEdit) and displays a warning if validation fails. It implements the standard `IDataErrorInfo` interface and applies a custom `ErrorControl` style to display icons (error, warning, information) along with descriptive messages to help users correct input errors. + + +![Indicate Errors and Warnings by Implementing IDataErrorInfo](./Images/validation.jpg) + +## Implementation Details + +### Create Validation Logic + +The data object implements the `IDataErrorInfo` interface. The `Error` property returns a formatted string that includes the error type and message: + +```csharp +public class TestClass : IDataErrorInfo { + public string TestString { get; set; } + + string IDataErrorInfo.Error { + get { return GetError(); } + } + + string GetError() { + if (string.IsNullOrEmpty(TestString)) + return "ErrorType=Critical;ErrorContent=The value is not provided. Please enter a value"; + if (TestString.Length < 3) + return "ErrorType=Warning;ErrorContent=The value is less than 3 characters. Please enter at least 5 characters"; + if (TestString.Length < 5) + return "ErrorType=Information;ErrorContent=The value is less than 5 characters. Please enter at least 5 characters"; + return string.Empty; + } + + string IDataErrorInfo.this[string columnName] { + get { + if (columnName == "TestString") + return GetError(); + return string.Empty; + } + } +} +``` + +### Parse Error Content + +The error string encodes multiple values (`ErrorType` and `ErrorContent`). A value converter extracts these parts and displays them in the UI: + + +```csharp +public class ErrorContentConverter : IValueConverter { + public string GetValueTag { get; set; } + public string Separator { get; set; } + + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { + if (value == null || !(value is string)) + return value; + string error = System.Convert.ToString(value, culture); + if (string.IsNullOrEmpty(error)) + return value; + + string searchString = GetValueTag + "="; + foreach (string suberror in error.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries)) { + if (suberror.Contains(searchString)) + return suberror.Replace(searchString, string.Empty); + } + return value; + } + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { + return null; + } +} +``` + +### Apply Two Converter Modes + +The converter works in **Type** and **Content** modes. You can extract the `ErrorType` or the `ErrorContent` from the `IDataErrorInfo.Error` string: + +* **Type** mode drives an implicit `ErrorControl` style that picks the appropriate icon (Critical/Warning/Information). +* **Content** mode feeds a custom tooltip that displays the error message next to the editor. + +```xaml + + + + + + + + + + + + + + + + + + +``` + +## Files to Review * [MainWindow.xaml](./CS/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/MainWindow.xaml)) * [MainWindow.xaml.cs](./CS/MainWindow.xaml.cs) (VB: [MainWindow.xaml.vb](./VB/MainWindow.xaml.vb)) - -# WPF Editors - Indicate errors and warnings by implementing IDataErrorInfo +## Documentation -

Please implement the IDataErrorInfo interface on the data object. Then, pass the text and type of error in the IDataErrorInfo.Error property ( it is possible to easily parse this string). Implement a custom style for the ErrorControl. This element presents the error icon. Modify the ErrorControl style in such a way as to take into account a custom error type and text (use the converter).


-

+* [TextEdit](https://docs.devexpress.com/WPF/DevExpress.Xpf.Editors.TextEdit) +* [EditValue](https://docs.devexpress.com/WPF/DevExpress.Xpf.Editors.BaseEdit.EditValue) +* [ErrorToolTipContentTemplate](https://docs.devexpress.com/WPF/DevExpress.Xpf.Editors.BaseEdit.ErrorToolTipContentTemplate) +* [ErrorContent](https://docs.devexpress.com/WPF/DevExpress.Xpf.Editors.Validation.BaseValidationError.ErrorContent) -
+## More Examples +* [WPF Data Editors - Create a Registration Form](https://github.com/DevExpress-Examples/wpf-data-editors-create-registration-form) +* [WPF Data Editors - Allow Users to Enter Only Positive Numbers](https://github.com/DevExpress-Examples/wpf-editors-prevent-negative-values) +* [WPF Data Grid - Use Custom Editors to Edit Cell Values](https://github.com/DevExpress-Examples/wpf-data-grid-use-custom-editors-to-edit-cell-values) +* [WPF Data Grid - How to Validate Cell Editors](https://github.com/DevExpress-Examples/wpf-data-grid-validate-cell-editors) -## Does this example address your development requirements/objectives? - -[](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-editors-validate-user-input-indicate-errors-idataerrorinfo&~~~was_helpful=yes) [](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-editors-validate-user-input-indicate-errors-idataerrorinfo&~~~was_helpful=no) - +## Does this example address your development requirements/objectives? + +[](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-editors-validate-user-input-indicate-errors-idataerrorinfo&~~~was_helpful=yes) [](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=wpf-editors-validate-user-input-indicate-errors-idataerrorinfo&~~~was_helpful=no) + (you will be redirected to DevExpress.com to submit your response)